我应该在这里使用awakeFromNib或initWithCoder? [英] Should I be using awakeFromNib or initWithCoder here?

查看:167
本文介绍了我应该在这里使用awakeFromNib或initWithCoder?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的初始视图控制器加载,我需要一个NSArray初始化,我应该在 awakeFromNib 方法或 initWithCoder:方法? awakeFromNib似乎工作更好,因为我不需要返回任何东西,但它作为nib文件是什么曾经被正确使用?我不想使用会很快崩溃的方法。

My initial view controller is loaded, and I need an NSArray to be init'd, should I take care of this in an awakeFromNib method or an initWithCoder: method? awakeFromNib seems to work nicer, as I don't need to return anything, but it works as nib files were what used to be used right? I don't want to use a method that will break soon.

并且 initWithCoder:看起来像:

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super initWithCoder:decoder]) {
        self.articles = [[NSMutableArray alloc] init];
    }

    return self;
}


推荐答案

-awakeFromNib 是这样的,当你可以确保所有与nib中的其他对象的连接已经建立时,你可以做init的东西。

The point of -awakeFromNib is so that you can do init stuff when you can be sure that all your connections to other objects in the nib have been established.


nib加载基础结构向从nib归档中重建的每个
对象发送一个awakeFromNib消息,但只有在
中的所有对象都已加载后,初始化。当一个对象接收到
awakeFromNib消息时,它保证所有的outlet和
动作连接已经建立。

The nib-loading infrastructure sends an awakeFromNib message to each object recreated from a nib archive, but only after all the objects in the archive have been loaded and initialized. When an object receives an awakeFromNib message, it is guaranteed to have all its outlet and action connections already established.

不要忘记呼叫超级

这不太可能很快就消失,这么多的代码使用它,过渡期会很长。是的,它的名称来自旧的nib文件格式,但这个堆栈溢出问题清除了文件扩展名的差异。

It is unlikely to go away any time soon, and if it did so much code uses it that the transition period would be long. Yes its name comes from the old "nib" file format but this stack overflow question clears up the differences in the file extensions.

因此,在总结任何一种方法将为您工作,该类的内部实例变量。注意,在 init 方法(包括 -initWithCoder )中,可能不安全使用setter方法在类完全初始化(源WWDC 2012视频移动到现代目标c)。一个例子是设置引用nib文件中另一个对象的属性。

So in summary either method will work for you as you are setting an internal instance variable for the class. Note that inside init methods (including -initWithCoder) it may not be safe to use your setter methods in case setters rely on the class being fully initialised (source WWDC 2012 video moving to modern objective-c). An example would be setting a property that references another object in the nib file.

UIViewController 子类 -initWithCoder 仅在从故事板加载时调用。因为 -awakeFromNib 被调用,无论你使用storyboard还是不使用它更有意义。

In UIViewController subclasses -initWithCoder is only called when loading from a storyboard. As -awakeFromNib is called whether you use storyboards or not it might make more sense to use that.

另一种模式你可以考虑是懒惰的getter:

Another pattern you could consider is the lazy-getter:

-(NSMutableArray *)articles{
    if (_articles){
        return _articles;
    }
    _articles = [[NSMutableArray alloc] init];
    return _articles;
}

这种方法的好处是,如果你想进一步设置数组,你可以很容易地丢弃数组,当你不再需要它,下一次访问该属性,你有一个新鲜的。

The benefit of this approach is that if you wanted to do further setup to the array you can easily discard the array when you don't need it anymore and the next time you access the property you have a fresh one again.

这篇关于我应该在这里使用awakeFromNib或initWithCoder?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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