开发者

Cannot access properties of subclass

I have created the following subclass to do some custom drawing:

//  DocumentIconView.h

@interface DocumentIconView : UIView
{
    UIImageView     *documentIconView;
    CloseHandle     *closeHandle;
    UILabel         *filenameLabel;
}

@property (nonatomic, strong) UIImageView *documentIconView;
@property (nonatomic, strong) CloseHandle *closeHandle;
@property (nonatomic, strong) UILabel *filenameLabel;

+ (DocumentIconView *)documentIconWithFrame:(CGRect)viewFrame
                     previewImage:(UIImage *)previewImage
                            title:(NSString *)title;

This works well for the most part (I can instantiate objects, and do custom drawing), however, I cannot access some of its properties from another classes.

DocumentIconView *iconView = [DocumentIconView documentIconWithFrame:frame 
                                                      previewImage:[UIImage imageNamed:@"GenericDocumentIcon.png"] 
                                                             title:[NSString stringWithFormat:@"test"]];

iconView.backgroundColor = [UIColor lightGrayColor];  // this works
iconView.filenameLabel.backgroundColor = [UIColor darkGrayColor]; // this does not work - no error message
[documentsView addSubview:iconView];

I can read and write first-level properties, but cannot drill deeper; when trying to read properties, the value returned is (null)

I am fairly new at subclassing, so I think I am missing something really obvious here. Any help would be greatly appreciated.

EDIT: the method for instantiating the view in question:

+ (DocumentIconView *)documentIc开发者_如何学编程onWithFrame:(CGRect)viewFrame
                     previewImage:(UIImage *)previewImage
                            title:(NSString *)title
{
    DocumentIconView *view = [[DocumentIconView alloc] initWithFrame:viewFrame];

    // Close handle's size is assigned here
    CGSize closeHandleSize = CGSizeMake(27, 27);

    // The document preview image's frame is calculated by shrinking it by the close handle's size
    CGRect documentPreviewFrame = CGRectMake(closeHandleSize.width / 2, 
                                           closeHandleSize.height / 2, 
                                           viewFrame.size.width - closeHandleSize.width, 
                                             viewFrame.size.height - closeHandleSize.height - 20); // 20 points is the filenameLabel's height

    UIImageView *documentPreviewView = [[UIImageView alloc] initWithFrame:documentPreviewFrame];
    documentPreviewView.contentMode = UIViewContentModeScaleAspectFit;
    documentPreviewView.backgroundColor = [UIColor clearColor];
    documentPreviewView.image = previewImage;

    [view addSubview:documentPreviewView];

    CGRect closeHandleFrame = CGRectMake(0, 0, closeHandleSize.width, closeHandleSize.height);
    CloseHandle *closeHandleView = [[CloseHandle alloc] initWithFrame:closeHandleFrame];

    closeHandleView.alpha = 0.0;
    closeHandleView.tag = kCloseHandleTag;
    [view addSubview:closeHandleView];

    CGRect filenameFrame = CGRectMake(0, 
                                      viewFrame.size.height - 20, 
                                      viewFrame.size.width, 
                                      20);
    UILabel *filenameLabel = [[UILabel alloc] initWithFrame:filenameFrame];
    filenameLabel.backgroundColor = [UIColor clearColor];
    filenameLabel.text = title;
    filenameLabel.font = [UIFont boldSystemFontOfSize:17];
    filenameLabel.textColor = [UIColor whiteColor];
    filenameLabel.textAlignment = UITextAlignmentCenter; 
    [view addSubview:filenameLabel];

    view.tag = kDocumentIconTag;

    return view;
}


In your documentIconWithFrame:... method you're using a local variable (filenameLabel) that you're adding to the view. That means your instance variable is never instantiated and is always nil.

Just change this:

UILabel *filenameLabel = [[UILabel alloc] initWithFrame:filenameFrame];

to this:

filenameLabel = [[UILabel alloc] initWithFrame:filenameFrame];

and the same for the other instance variables.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜