NSWindowController showWindow:nil什么都不做

aka*_*kyy 10 macos cocoa objective-c nswindow nswindowcontroller

喜欢在标题中,[myWindowController showWindow:nil]不起作用.以下是您可能需要了解的一些事实:

  • 我的窗口控制器: KRAuthenticationWindowController
  • 接口构建器文件: AuthenticationWindow.xib
  • 文件的所有者是 KRAuthenticationWindowController
  • window 插座连接到窗口
  • Window delegate已连接到File的所有者
  • 窗口Visible at launch是未选中的
  • 窗口Release when closed也是未选中的

我的代码如下:

// KRApplicationDelegate.m

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    NSLog(@"%s",__PRETTY_FUNCTION__);
    KRAuthenticationWindowController *authWindowController = [[KRAuthenticationWindowController alloc] init];
    [authWindowController showWindow:nil];
    [[authWindowController window] makeKeyAndOrderFront:nil];
}

// KRAuthenticationWindowController.m

- (id)init {
    self = [super initWithWindowNibName:@"AuthenticationWindow"];
    if(!self) return nil;
    NSLog(@"%s",__PRETTY_FUNCTION__);
    return self;
}

- (void)loadWindow {
    [super loadWindow];
    [self.window setBackgroundColor:[NSColor colorWithDeviceWhite:0.73 alpha:1]];
    NSLog(@"%s",__PRETTY_FUNCTION__);
}

- (void)windowDidLoad {
    [super windowDidLoad];
    NSLog(@"%s",__PRETTY_FUNCTION__);
}

- (void)showWindow:(id)sender {
    [super showWindow:sender];
    NSLog(@"%@",self.window);
    NSLog(@"%s",__PRETTY_FUNCTION__);
}
Run Code Online (Sandbox Code Playgroud)

我的控制台输出:

2013-02-24 16:21:45.420 Application[3105:303] -[KRApplicationDelegate applicationDidFinishLaunching:]
2013-02-24 16:21:45.421 Application[3105:303] -[KRAuthenticationWindowController init]
2013-02-24 16:21:45.428 Application[3105:303] -[KRAuthenticationWindowController loadWindow]
2013-02-24 16:21:45.428 Application[3105:303] -[KRAuthenticationWindowController windowDidLoad]
2013-02-24 16:21:45.556 Application[3105:303] <NSWindow: 0x10016e860>
2013-02-24 16:21:45.556 Application[3105:303] -[KRAuthenticationWindowController showWindow:]
Run Code Online (Sandbox Code Playgroud)

我想我只是遗漏了一些重要的东西.任何帮助,将不胜感激.

use*_*453 33

尝试将authWindowController转换为实例变量.目前,它是一个局部变量.当局部变量消失时,窗口控制器可能会被释放并且窗口随之被释放,因此它永远不会被显示出来.

  • 是的,这就是诀窍.非常感谢你!:) (2认同)