我创建了一个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。知道为什么吗?
答案 0 :(得分:3)
galleryImageView
是一个IBOutlet,只有在从nib加载后才会获取内存。在它之前执行init方法.Hence 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];
}