3

我想在我的應用程序委託中使用NSWindowController打開一個窗口。 我創建了一個基本NSWindowController具有相關的NIB,並嘗試以顯示窗口的方式:NSWindowController的窗口立即發佈

@implementation MyAppDelegate 
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    // Show the main window from a separate nib 
    MyWindowController * theWindowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"]; 
    [theWindowController showWindow:self]; 
} 
@end 

當我啓動應用程序,MyWindowController的窗口只出現第二分數(似乎是爲即將發佈當它啓動時)。

使用ARC,我怎麼能強制窗口粘住而不是立即刷新?我不使用NSDocuments,我希望能夠同時使用許多這些MyWindowController。

回答

11

您需要將屬性添加到保留WindowConroller的應用程序委託(或其他一些將保留應用程序的生命週期的對象)中。例如:

@interface MyAppDelegate : NSObject 

@property (strong, nonatomic) MyWindowController * windowController; 

@end 

然後在初始化窗口控制器時設置此屬性。

@implementation MyAppDelegate 

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification 
{ 
    // Show the main window from a separate nib 
    self.windowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindowController"]; 
    [theWindowController showWindow:self]; 
} 

@end 
+0

哎喲,我剛纔問過那個......謝謝;) – 2012-08-15 15:35:26

+0

如果我想用windowController打開相同的窗口怎麼辦?當我打電話給第二個第一個立即釋放。如果我創建windowController1它的作品,但我想創造更多。 – mohacs 2013-02-11 20:45:52

+0

@mohacs您需要保存您呈現的每個窗口控制器。爲第二個創建新的屬性。需要創造更多?創建'NSMutableArray'並將它們添加到那裏。 – Ossir 2015-11-28 18:37:00