2017-02-04 39 views
0

我已經使用此代碼來配置窗口。它已經工作過。爲什麼我每次訪問`self.window`時都加載了我的窗口

- (void)awakeFromNib 
{ 
    NSLog(@"self = %p", self); 
    [(NSPanel *)self.window setWorksWhenModal: NO]; 
} 

無論如何,每次我訪問self.window窗口是從筆尖加載。這是一個問題,因爲它使這個遞歸。但它在其他地方也是一個問題,因爲我每次都會得到不同的窗口!

enter image description here

從 「NSWindowController」:

/* The window getter will load the nib file (if there is one and it has not yet been loaded) and then return the window. 
If it has to load the window, it will first call -windowWillLoad, then -loadWindow, then -windowDidLoad. 
To affect nib loading or do something before or after it happens, you should always override those other methods. 

     The window setter is used internally from -initWithWindow: or when a controller's nib file is loaded (as the "window" outlet gets connected). 
You can also call it yourself if you want to create the window for a window controller lazily, but you aren't loading it from a nib. 
This can also be used to set the window to nil for cases where your subclass might not want to keep the window it loaded from a nib, but rather only wants the contents of the window. 
Setting the window to nil, after the nib has been loaded, does not reset the -isWindowLoaded state. 
A window controller will only load its nib file once. This method makes sure the window does not release when closed, and it sets the controller's -windowFrameAutosaveName onto the window and updates the window's dirty state to match the controller's document (if any). 
It also calls -setWindowController: on the window. You can override this if you need to know when the window gets set, but call super. 
    */ 
    @property (nullable, strong) NSWindow *window; 

回答

1

都在自己的窗口NIB哪些對象?我懷疑你已經在NIB中創建了你的窗口控制器類的一個實例。因此,無論何時加載該NIB(可能通過您在代碼中創建的窗口控制器類的實例),都會創建窗口控制器的新實例。該新實例接收到-awakeFromNib並請求它的window,這會導致它加載另一個NIB實例,並重復該過程。

窗口控制器不應該在窗口NIB中實例化。 NIB中的文件所有者佔位符應配置爲具有窗口控制器的類。窗口控制器應該在代碼中實例化並初始化,以便它自己作爲NIB的所有者。這將使它填補File's Owner持有它的地方。

此外,你應該避免覆蓋-awakeFromNib。它可以被意外地調用。對於這些類型的任務覆蓋-windowDidLoad通常更安全。

+0

哦,就是這樣。我在窗口NIB中有控制器對象!謝謝! – netigger

+0

或等待,讓我再次驗證這個..剛剛重新創建它,現在在NIB沒有任何對象,但仍然得到它,生病回來。 – netigger

+0

我其實還是有問題的。但是我仍然在從awakeFromNib訪問'self.window',但是現在看起來效果很好,現在我也刪除了它 – netigger

相關問題