2013-04-02 72 views
0

我已經創建了一個自定義視圖,並在自定義視圖內,有一個imageView。當我將控件傳遞給customView並打印出我的imageView時,它將返回null。不知道爲什麼。UIImageView返回零自定義視圖

.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

在IB或代碼創建的視圖?如果在代碼中創建,你是否將其添加到視圖?你掛了電源插座嗎? –

+0

'galleryImageView'本身是否返回null或其圖像是否爲null? –

+0

我在IB創建了一個視圖,並提出了自定義類= GalleryImage – lakesh

回答

3

galleryImageView是一個IBOutlet它得到它從筆尖.The 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]; 
} 
+0

謝謝..得到的意思..說的init方法在nib文件初始化之前調用... – lakesh

+0

如何解決問題? – lakesh

+0

@lakesh編輯答案 –

相關問題