使用自定义initWithCoder初始化视图 [英] Initializing a view with custom initWithCoder

查看:115
本文介绍了使用自定义initWithCoder初始化视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了初始化具有xib的视图,我使用 initWithCoder 函数。但是,如果我需要使用自定义参数初始化xib,该怎么办?

In order to initialize a view that has xib, I use initWithCoder function. But what if I need to initialize the xib with custom parameter.

我需要这样的东西:

- (id)initWithCoder:(NSCoder *)aDecoder
           andTitle:(NSString *)titleString
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        self.titleLabel = titleString;
    }
    return self;
}

我什么时候打电话给它? 之后awakeFromNib

And when do I call it? After awakeFromNib?

推荐答案

您无法修改 initWithCoder:这样的方法,因为该方法是在您无法控制的协议中定义的。相反,您需要在创建对象后调用 setTitle:方法,可能在 awakeFromNib 中,或者从拥有控制器。

You can't modify the initWithCoder: method like that because the method is defined in a protocol you don't control. Instead you need to either call the setTitle: method after the object has been created, possibly in awakeFromNib, or from the owning controller.

这篇关于使用自定义initWithCoder初始化视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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