代码之家  ›  专栏  ›  技术社区  ›  BigCola

如何使用界面生成器管理Cocoa应用程序中的多个窗口

  •  4
  • BigCola  · 技术社区  · 13 年前

    我有一个包含3个类的应用程序:AppController、Profile、ProfileBuilder。我还需要3个窗口:每节课一个。我尝试将所有3个都保留为NSObject的子类,并将initWithNibName应用于NSWindowController类WindowController变量,但当我尝试在每个窗口上输出一些值时,它不起作用,而且使用NSLog导致窗口为null。我想知道管理多个窗口的最佳方式是什么,也许都来自同一个类,比如AppWindowsController,在其他类中涉及尽可能少的特定代码,并在可能的情况下将其他类保留为NSObject的子类,而不是NSWindowController。因此,如果有,也许有一种方法可以远程控制窗口的行为,在特定的类中添加尽可能少的代码,只是为了让它们尽可能清晰,并唯一地关注它们的内容。谢谢,希望我能说清楚,实际上我对Cocoa框架还很陌生。

    2 回复  |  直到 13 年前
        1
  •  6
  •   rdelmar    13 年前

    您应该能够在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;
    }
    

    profileWindow是一个属性(类型为strong)。在xib文件中,我将文件的所有者设置为Profile。

        2
  •  2
  •   Stephan    13 年前

    我只是想改进rdelmar的解决方案。

    您不需要迭代数组就可以找到 NSWindow 班 如果您将profileWindow定义为出口并将其连接到IB中,则调用

    [[NSBundle mainBundle] loadNibNamed:@"ProfileWindow" owner:self topLevelObjects:&array];
    

    将窗口对象分配给你的出口,数组的东西是不需要的。 这里的关键是作为接口的所有者对象。在IB中,您可以定义所有者的类类型,如果是,请查看其出口。