2013-02-24 107 views
10

和標題一樣,[myWindowController showWindow:nil]不起作用。這裏有您需要知道的一些事實:NSWindowController showWindow:nil什麼都不做

  • 我的窗口控制:KRAuthenticationWindowController
  • 界面生成文件:AuthenticationWindow.xib
  • 文件的所有者是KRAuthenticationWindowController
  • window出口連接到窗口
  • 窗口的delegate連接到文件的所有者
  • 窗口的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__); 
} 

我的控制檯輸出:

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:] 

我想我只是缺少一些重要的東西。任何幫助,將不勝感激。

+0

也許你有這個問題中描述的問題http://stackoverflow.com/questions/3539721/nswindowcontroller-loadwindow-loads-window-from-nib-but-showwindow-does-nothin – sergeyne 2013-02-24 18:30:40

+0

不,不是這樣。 – akashivskyy 2013-02-24 22:08:59

回答

31

嘗試將authWindowController轉換爲實例變量。目前,它是一個局部變量。當局部變量消失時,窗口控制器可能會被釋放,窗口也會被釋放,所以它永遠不會被顯示。

+1

這可能會發生,如果你正在使用ARC,在這個問題的更多細節http://stackoverflow.com/questions/11677043/nswindowcontrollers-window-released-immediately – sergeyne 2013-02-25 07:16:06

+2

是的,這個伎倆。非常感謝你! :) – akashivskyy 2013-02-25 14:44:34

+0

剛纔碰到同樣的問題,謝謝你的建議 – ribeto 2015-05-20 02:29:43