0

我创建了一个customView,在customView 里面有一个imageView。当我将控件传递给 customView 并打印出我的 imageView 时,它返回 null。不知道为什么。

。H

#import <UIKit/UIKit.h>

@interface GalleryImage : UIView
{
    IBOutlet UIImageView *galleryImageView;
}
- (id)initWithImage:(UIImage*) image;
@property(strong,nonatomic) IBOutlet UIImage *galleryImage;
@end

.m

#import "GalleryImage.h"

@implementation GalleryImage

@synthesize galleryImage;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
- (id)initWithImage:(UIImage*) image
{
    self = [super init];
    if (self) {
        galleryImage = image;
        [galleryImageView setImage:galleryImage];
    }
    return self;
}

以这种方式传递控制:

UIImage *infoImage = [UIImage imageNamed:imageName];

GalleryImage *galleryItem = [[GalleryImage alloc] initWithImage:infoImage];

但我的galleryImageView 返回null。知道为什么吗?

4

1 回答 1

3

galleryImageView是一个 IBOutlet,它只有在从 nib 加载后才获得内存。init 方法在它之前执行。因此 imageview 没有有效的内存,它返回 null

在里面做

awakeFromNib
Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.

所以用这个

- (void)awakeFromNib {
        [galleryImageView setImage:galleryImage];
}
于 2013-04-02T18:27:30.253 回答