初始化NSWindowController子类 [英] initializing an NSWindowController subclass

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

问题描述

根据Apple的文档,初始化NSWindowController子类的推荐方法是调用init()而不是initWithWindowNibName()。该文档继续说,由于NSWindowController可能只能与为其设计的笔尖一起使用,因此让子类调用super initWithWindowNibName,并且如果任何initWithWindowNib ...方法都存在,则子类应记录错误。

According to Apple's documentation, the recommended way of initializing an NSWindowController subclass is by calling init() and NOT initWithWindowNibName(). The documentation goes on to say that since an NSWindowController is likely only going to work with the nib it was designed for, then have the subclass call the super initWithWindowNibName, and the subclass should log an error if any of the initWithWindowNib... methods are called.

所以这是我写的:

- (id) init
{
    NSLog(@"init()");
    return [super initWithWindowNibName:@"MyDocument"];
}

- (id) initWithWindowNibName:(NSString *)windowNibName
{
    NSLog(@"error...use init() instead");
    return nil;
}

- (id) initWithWindowNibName:(NSString *)windowNibName owner:(id)owner
{
    NSLog(@"error...use init() instead");
    return nil;
}

- (id) initWithWindowNibPath:(NSString *)windowNibPath owner:(id)owner
{
    NSLog(@"error...use init() instead");
    return nil;
}

运行时,我看到的是输出:

When it runs, I see as output:

init()
error...use init() instead

所以...嗯?发生了什么事?

So...huh? Whats going on?

有一个关于两次init()的stackoverflow问题,其解决方案是一个实例是通过代码创建的,另一个实例是通过笔尖创建的。我的笔尖根本没有控制器对象。

There's a stackoverflow question about init() being called twice, with the resolution being that one instance was being created via code, and the other via a nib. My nib has no controller objects in it at all.

推荐答案

问题是 [super initWithWindowNibName: @ MyDocument] 只是一种方便的方法。它只是调用 [self initWithWindowNibName:@ MyDocument owner:self] 。当然,这会引发您的错误消息。您可以通过将 init 方法更改为以下方法来解决此问题:

The problem is that [super initWithWindowNibName:@"MyDocument"] is just a convenience method. What it does is just call [self initWithWindowNibName:@"MyDocument" owner:self]. This of course throws your error message. You can fix this by just changing your init method to:

- (id) init
{
    NSLog(@"init()");
    return [super initWithWindowNibName:@"MyDocument" owner:self];
}

这篇关于初始化NSWindowController子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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