2017-02-04 48 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

相关问题