如何使用Interface Builder管理Cocoa应用程序中的多个窗口

Big*_*ola 4 xcode cocoa window nswindow

我有3个类的应用程序:AppController,Profile,ProfileBuilder.我还需要3个窗口:每个班级一个.我尝试将所有3作为NSObject的子类并将initWithNibName应用于NSWindowController类WindowController变量但是当我尝试在每个窗口上输出一些值时它不起作用,并且使用NSLog将窗口生成为null.我想知道管理多个窗口的最佳方法是什么,可能所有来自同一个类,如AppWindowsController,尽可能少地涉及其他类中的特定代码,并且如果可能的话,保留其他类作为NSObject的子类而不是NSWindowController .因此,如果存在,可能是一种远程控制窗口行为的方法,在特定类中添加尽可能少的代码,只是为了使它们尽可能清晰并专注于其内容.谢谢,希望我明确表示,我实际上是Cocoa框架的新手.

rde*_*mar 6

您应该能够在不同类的init方法中使用windows加载nib文件.例如,在Profile中,您可以执行以下操作:

-(id)init {
    if (self = [super init]) {
        NSArray *array;
        BOOL success = [[NSBundle mainBundle] loadNibNamed:@"ProfileWindow" owner: self topLevelObjects:&array];
        if (success) {
            for (id obj in array) {
                if ([obj isKindOfClass:[NSWindow class]]) {
                    self.profileWindow = obj;
                }
            }
            [self.profileWindow makeKeyAndOrderFront:self];
        }
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

profileWindow是一个属性(输入为strong).在xib文件中,我将File的所有者设置为Profile.