为什么我的awakeFromNib不会开火

为什么我的awakeFromNib不会开火

本文介绍了为什么我的awakeFromNib不会开火?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始将更多的视图层次结构移动到IB。

I am starting to move more of my view hierarchy construction to IB.

我有一个名为AlignmentViewController.xib的nib文件,我在其中使用AlignmentViewController作为文件所有者设置我的视图层次结构。这很好。

I have a nib file called "AlignmentViewController.xib" in which I set up my view hierarchy with AlignmentViewController as the files owner. This works fine.

我仍然模糊的方法之一是awakeFromNib。在AlignmentViewController的以下代码片段中,我添加了一个额外的方法awakeFromNib。但是,它不会被调用:

One of the methods I remain fuzzy on is awakeFromNib. In the follow code snippet of AlignmentViewController I add the single additional method awakeFromNib. However, it does NOT get called:

- (void)awakeFromNib {

   NSLog(@"AlignmentViewController - awakeFromNib");
   [super awakeFromNib];

}

- (id)initWithChromosomeName:(NSString *)chromosomeName
               basepairStart:(NSUInteger)basepairStart
                 basepairEnd:(NSUInteger)basepairEnd {

    self = [self initWithNibName:@"AlignmentViewController" bundle:nil];

    if (nil != self) {


        self.title          = @"Alignment";

        self.chromosomeName = chromosomeName;
        self.basepairStart  = basepairStart;
        self.basepairEnd    = basepairEnd;

        self.wantsFullScreenLayout = YES;

    }

    return self;
}

有人可以向我澄清我错过了什么吗?

Can someone clarify for me what I have missed here?

干杯,
道格

更新:
读完一些答案后,我意识到我需要得到在一些非常基础的东西的底部。

UPDATE:After reading some the the answers I realize I need to get to the bottom of something that is pretty fundamental.

查看初始化:

- (id)initWithChromosomeName:(NSString *)chromosomeName
               basepairStart:(NSUInteger)basepairStart
                 basepairEnd:(NSUInteger)basepairEnd {

    self = [self initWithNibName:@"AlignmentViewController" bundle:nil];

我注意到我正在调用initWithNibName:bundle:indirect。这是不好的做法吗? AlignmentViewController.xib文件定义了我的视图层次结构。但我不是真的使用initWithNibName:bundle:以典型的方式?有更好的方法吗?

I notice I am calling initWithNibName:bundle: indirectly. Is this bad practice? The AlignmentViewController.xib file defines my view hierarchy. But I'm not really using initWithNibName:bundle: in the typical way? Is there a better way to do this?

推荐答案

-awakeFromNib 是在实例化所有nib对象之后,发送到从nib实例化的所有对象。所有者不是这些对象之一。它在反序列化nib之前存在并在加载后连接到nib,因此它不会被发送 -awakeFromNib (除非它当然是由其他一些nib本身实例化的)。

-awakeFromNib is sent to all object that are instantiated from the nib after all nib objects have instantiated. The owner is not one of those objects. It exists prior to deserializing the nib and is wired to the nib after loading, so it is not sent -awakeFromNib (unless of course it is instantiated by some other nib itself).

这里的一个线索是, -initFromChromosomePair:basepairStart:basepairEnd: -awakeFromNib 都可以被调用。 nib加载器始终调用 -initWithCoder: -initWithFrame: -init (其规则发送在)。您有一些其他指定的初始化程序这一事实强烈建议您在代码中的某处手动创建此对象,这意味着此对象不会从笔尖中唤醒。

One clue here is that there's no way both -initFromChromosomePair:basepairStart:basepairEnd: and -awakeFromNib could both be called. The nib loader always calls -initWithCoder:, -initWithFrame: or -init (the rules for which is sent are in The Nib Object Life Cycle). The fact that you have some other designated initializer strongly suggests that you're manually creating this object somewhere in the code, which means this object is not "awaken from nib."

这篇关于为什么我的awakeFromNib不会开火?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:44