可可-不叫awakeFromNib [英] Cocoa - awakeFromNib is not called

查看:64
本文介绍了可可-不叫awakeFromNib的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MainMenu.xib,而AppController是它的文件所有者.我添加了-(void)awakeFromNib 方法,效果很好.现在,在 awakeFromNib 路上进行的各种修复都停止了,我不知道为什么.它拥有xib,因此在未存档时应调用它.发生了什么事?

I have an MainMenu.xib, and AppController is its file owner. I added -(void)awakeFromNib method which worked fine. Now, rounds of fixings down the road awakeFromNib stopped being called, and I can't figure out why. It owns the xib, so it should be called when it is unarchived. What's going on?

已编辑

好吧,我将 awakeFromNib 重命名为 something ,然后从 init 调用了它.仍然对为什么 awakeFromNib 不是这样感到困惑.我那里也有一个 +(void)initialize 方法,这可能会使事情搞砸吗?

Well, I renamed awakeFromNib to something, and called that from init... that worked. Still confused as to why awakeFromNib is not. I also have a +(void) initialize method in there, could that be messing something up?

- (id)init {
    self = [super init];
    if (self) {
        [self something];
    }
    return self;
}


-(void)something {
    NSLog(@"yup");
}

推荐答案

在笔尖中设置文件所有者的类名仅是为了让您可以告诉Xcode显示对象的出口和操作,以便进行关联.当应用程序运行并且笔尖被加载时,它不会影响实际上文件的所有者.

Setting the class name of the File's Owner in the nib is only so you can tell Xcode what object's outlets and actions to show you so you can hook things up. It doesn't affect what object is actually the File's Owner when the app runs and the nib gets loaded.

MainMenu笔尖的文件所有者始终是应用程序对象,无论您在Xcode的检查器中为FO设置了什么类名.将其设置为除NSApplication [1]之外的任何类名是错误的.

The MainMenu nib's File's Owner is always the application object, no matter what class name you set for the FO in Xcode's inspector. Setting it to any class name but NSApplication[1] is wrong.

运行应用程序时,应该在控制台中找到有关尝试连接的AppController的所有插座或操作的错误消息.它们无法连接,因为应用程序对象没有它们.

When you run your app, you should find error messages in the Console about any outlets or actions of the AppController that you tried to connect. They couldn't be connected because the application object doesn't have them.

重新在nib编辑器中更改类名称,并在MainMenu nib中将AppController创建为自定义对象.

Change the class name back in the nib editor, and create your AppController as a custom object in the MainMenu nib.

好吧,我将 awakeFromNib 重命名为 something ,然后从 init ...调用了它.

Well, I renamed awakeFromNib to something, and called that from init... that worked.

这意味着将调用 init ,这意味着您正在调用它.这是在笔尖中创建它的有效替代方法,但是如果不在笔尖中或不拥有 awakeFromNib ,则不应该覆盖它.

That means that init is getting called, which means you're calling it. That's a valid alternative to creating it in a nib, though you shouldn't override awakeFromNib if it's not in a nib or owning one.

您的选择:继续使用 alloc init 创建AppController,或者删除该代码并在MainMenu笔尖中创建它.

Your choice: Continue creating the AppController using alloc and init, or remove that code and create it in the MainMenu nib instead.

[1]:或者,如果您将NSApplication子类化,并将应用程序捆绑包的主体类更改为该子类,则该子类的名称.

[1]: Or, if you've subclassed NSApplication and changed the principal class of your app bundle to be that subclass, the name of that subclass.

这篇关于可可-不叫awakeFromNib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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