IBOutlet在自定义UIView中为零(使用STORYBOARD) [英] IBOutlet is nil inside custom UIView (Using STORYBOARD)

查看:94
本文介绍了IBOutlet在自定义UIView中为零(使用STORYBOARD)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的UIView类.在其中,我声明了UIImageViewIBOutlet属性.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆