故事板加载时首先调用什么方法?

发布于 2024-12-17 08:51:22 字数 426 浏览 2 评论 0原文

使用 Xcode 4.2,在我的应用程序中,视图加载由 segue 事件触发。 在视图控制器中首先调用哪个方法?

-(void) viewWillAppear:(BOOL)animated 有效,但它是第一个吗?

初始化发生在 Storyboard 中,看起来,在创建对象时,永远不会手动调用 init 方法。

让我澄清一下,当手动创建类的实例时,我们通常首先[[alloc]init]它。在本例中,[init] 是第一个要执行的方法,也是进行各种初始化的好地方。

当类实例化通过 segue 事件发生时,init 方法的等效项是什么?在这种情况下,什么方法应该包含所有初始化逻辑?

Using Xcode 4.2, in my application, a view loading is triggered by a segue event. What method will be called first inside a view controller?

-(void) viewWillAppear:(BOOL)animated works, but is it the first?

Initialization happens from the Storyboard it seems, init method is never manually called, upon object creation.

Let me clarify, when creating an instance of a class manually, we usually [[alloc]init] it first. [init] in this case, is the first method to be executed and a good place for various initializations.

What is the equivalent of init method when class instantiation happens via a segue event? In such a case, what method should contain all initialization logic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情徒 2024-12-24 08:51:22

我认为最好的选择是 -(void)awakeFromNib。这只发生一次,而 viewWillAppearviewDidLoad 等在初始化后可能会被多次调用。

更新:正如下面 Jean-Denis Muys 所指出的,对于初始化程序来说, -(id)initWithCoder:(NSCoder *)decoder 是更好的选择,因为 -(void) 只被调用一次awakeFromNib 有可能被多次调用。

I think the best option is -(void)awakeFromNib. This only occurs the once, whereas viewWillAppear and viewDidLoad etc could be called more than once after your initialisation.

UPDATE: As pointed out by Jean-Denis Muys below, -(id)initWithCoder:(NSCoder *)decoder is a better option for an initialiser that only gets called once as -(void)awakeFromNib has the potential to be called more than once.

一抹淡然 2024-12-24 08:51:22

根据苹果的 查看控制器编程指南,

当您在情节提要中创建视图控制器时,您所使用的属性
Interface Builder 中的配置在存档中序列化。之后,
当视图控制器被实例化时,这个存档被加载到
记忆并处理。结果是一组对象,其属性
与您在 Interface Builder 中设置的相匹配。存档的加载方式为
调用视图控制器的 initWithCoder: 方法。然后,
awakeFromNib 方法在实现该方法的任何对象上调用。
您可以使用此方法执行任何配置步骤
要求其他对象已经实例化。

According to Apple's View Controller Programming Guide,

When you create a view controller in a storyboard, the attributes you
configure in Interface Builder are serialized in an archive. Later,
when the view controller is instantiated, this archive is loaded into
memory and processed. The result is a set of objects whose attributes
match those you set in Interface Builder. The archive is loaded by
calling the view controller’s initWithCoder: method. Then, the
awakeFromNib method is called on any object that implements that method.
You use this method to perform any configuration steps that
require other objects to already be instantiated.

朕就是辣么酷 2024-12-24 08:51:22

我建议不要使用 awakeFromNib。我只使用这两个函数

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (void)setupButton {
    /* get ready to be called twice */
    self.layer.cornerRadious = 10.0f;
}

因为:假设您对 UIButton 进行了子类化。您必须准备好两种场景

场景 1。如果您以编程方式添加按钮 --> initWithFrame->setupUI 将被调用
场景2.如果使用NIb添加按钮--> initWithCoder->setupUI 将被调用。

I would advise not to use awakeFromNib. I use simply both of these functions

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setup];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (void)setupButton {
    /* get ready to be called twice */
    self.layer.cornerRadious = 10.0f;
}

because: let's say you subclassed UIButton. You must be ready for two scenario:

scenario 1. If you add the button programmatically --> initWithFrame->setupUI will be called
scenario 2. If you add the button using NIb --> initWithCoder->setupUI will be called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文