IBOutlet在自定义UIView中为零(使用STORYBOARD) [英] IBOutlet is nil inside custom UIView (Using STORYBOARD)
问题描述
我有一个自定义的UIView
类.在其中,我声明了UIImageView
的IBOutlet
属性.
I have a custom UIView
class. Inside it I have declared an IBOutlet
property for UIImageView
.
#import <UIKit/UIKit.h>
@interface SettingItem : UIView{
}
@property (strong, nonatomic) IBOutlet UIImageView *myImage;
@end
现在我正在使用情节提要.有一个视图控制器.我将UIView
拖动到viewcontroller.我拖动了一个UIImageView
作为UIView
上方的子视图.我从情节提要中将"SettingItem"类设置为UIView
.我是通过从实用程序窗口中的SettingItem
插座正常拖动将插座连接到myImage的.
Now i am using storyboard. There is a viewcontroller. I dragged UIView
to viewcontroller. I dragged one UIImageView
as a subview of above UIView
. I set the "SettingItem" class to UIView
from storyboard. I connected the outlet to myImage by normal dragging from outlets of SettingItem
from utilities window.
#import "SettingItem.h"
@implementation SettingItem
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self baseInit];
}
return self;
}
-(id) initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
// Initialization code
[self baseInit];
}
return self;
}
- (void) baseInit{
NSLog(@"myImage %@"self.myImage);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
现在我的问题是myImage
总是nil
,在NSLog
以上,只需在插座上打印(null)
.我检查了情节提要中的视图,并检查了它的出口引用及其指向myImage
的指向.我想念一些东西.我在Google上寻求帮助,但找不到任何解决方案.
Now my problem is myImage
is always nil
, above NSLog
just print (null)
for the outlet. I checked the view in storyboard and checked its outlet reference and its pointing to myImage
. I am missing something. I googled for the help but couldn't find any solution.
您能指出我在做什么错吗?
Can you please point out what am i doing wrong ?
推荐答案
在您的视图中覆盖awakeFromNib-这是在分配IBOutlet之后要调用的第一个生命周期方法,并且是您的代码的正确位置.
Override awakeFromNib in your view - this is the first lifecycle method to be called after the IBOutlets have been assigned and is the right place for your code.
这篇关于IBOutlet在自定义UIView中为零(使用STORYBOARD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!