使用NSWindowController时出错

use*_*mho 5 cocoa objective-c nswindowcontroller

我很久以后就开始了一个新的Cocoa项目......我不知道为什么,但是当我通过NSWindowController调用xib时总是会出错.我所做的非常简单:我有一个新项目作为起点,然后我不想从Appdelegate调用xib,而是从NSWindowController的子类调用.然后输出告诉我:

2014-11-12 09:58:18.519 SimpleTest [8554:378690] ApplePersistence = NO

2014-11-12 09:58:18.671 SimpleTest [8554:378690]无法连接(窗口)出口(NSApplication)到(NSWindow):缺少setter或实例变量

好的,它在代码中看起来如何?我的Appdelegate看起来像这样:

#import "AppDelegate.h"
#import "MainWindowController.h"

@interface AppDelegate ()

@property (weak) IBOutlet NSWindow *window;
@property (strong) MainWindowController *mainWindowController;

@end

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    _mainWindowController = [[MainWindowController alloc] initWithWindowNibName:@"MainMenu"];
    [self.mainWindowController showWindow:self];
}

@end
Run Code Online (Sandbox Code Playgroud)

到目前为止没什么特别的.该MainWindowController如下所示:

#import "MainWindowController.h"

@interface MainWindowController ()
@property (weak) IBOutlet NSWindow *window;

@end

@implementation MainWindowController

- (id)initWithWindow:(NSWindow *)window
{
    self = [super initWithWindow:window];

    if (self != nil)
    {
        //do something
    }

    return self;
}

@end
Run Code Online (Sandbox Code Playgroud)

再次非常简单...另外我在IB中有一些修改:MainMenu.xib的File'Owner变为MainWindowController.它的"窗口"插座连接到应用程序的窗口.Window的代表连接到File的所有者.好吧,就是这样!但为什么我会收到此错误?我究竟做错了什么?

---编辑---这显示了IB中的连接

conncections

Mar*_*k H 2

最重要的是使用正确的选择器创建新实例并正确连接所有内容。

步骤: 1. 添加新的带有窗口或空的 xib,并向其中添加窗口

在此输入图像描述

2.选择文件所有者并将其设置为NSWindowController或其子类。

在此输入图像描述

  1. 选择窗口并将其连接到文件所有者

在此输入图像描述

  1. 创建新的 NSWindowController 实例

  - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
  {
      NSWindowController *windowController = [[NSWindowController alloc] initWithWindowNibName:@"Window"];
      [NSApp runModalForWindow:[windowController window]];
  }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述