我刚开始了一个新的可可项目.我不知道为什么,但是当NSWindowController调用xib时总是会出现错误。我所做的事情非常简单:我有一个新的项目作为起点,然后我不想从Appdelegate调用xib,而是从NSWindowController的子类调用xib。然后输出告诉我:
2014-11-12 09:58:18.519 SimpleTest8554:378690 ApplePersistence=NO 2014-11-12 09:58:18.671 SimpleTest8554:378690连接(窗口)出口(NSApplication)到(NSWindow):缺少setter或实例变量
好吧,密码看上去怎么样?我的应用程序代表如下:
#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到目前为止没什么特别的。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再简单不过了..。另外,我在IB中做了一些修改: MainMenu.xib的文件所有者变成了MainWindowController。它的“窗口”出口连接到应用程序的窗口。窗口的委托连接到文件的所有者。好吧,就这样!但是为什么我会收到这个错误呢?我做错了什么?
--编辑--这显示了IB中的连接

发布于 2018-11-13 13:07:24
最重要的是使用正确的选择器来创建新的实例,并且所有连接都是正确的。
步骤: 1.添加带有窗口或空窗口的新xib并向其添加窗口

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


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

https://stackoverflow.com/questions/26886128
复制相似问题