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
.我错过了一些东西.我用谷歌搜索帮助但找不到任何解决方案.
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 - 这是分配 IBOutlets 后要调用的第一个生命周期方法,并且是您代码的正确位置.
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屋!