Macos 为什么每次访问“self.window”时都会加载我的窗口`

Macos 为什么每次访问“self.window”时都会加载我的窗口`,macos,cocoa,nswindow,Macos,Cocoa,Nswindow,我已经使用此代码配置了一个窗口。它以前曾经奏效过 - (void)awakeFromNib { NSLog(@"self = %p", self); [(NSPanel *)self.window setWorksWhenModal: NO]; } 无论如何,每次我访问self.window时,窗口都是从nib加载的。这是一个问题,因为它使这个递归。但这在其他地方也是个问题,因为我每次都会得到一个不同的窗口 从“NSWindowController”: 窗口笔尖中有哪些对象?

我已经使用此代码配置了一个窗口。它以前曾经奏效过

- (void)awakeFromNib
{
    NSLog(@"self = %p", self);
    [(NSPanel *)self.window setWorksWhenModal: NO];
}
无论如何,每次我访问
self.window
时,窗口都是从nib加载的。这是一个问题,因为它使这个递归。但这在其他地方也是个问题,因为我每次都会得到一个不同的窗口

从“NSWindowController”:


窗口笔尖中有哪些对象?我怀疑您已经在NIB中创建了窗口控制器类的实例

因此,每当加载该NIB时(可能通过在代码中创建的窗口控制器类的实例),都会创建一个新的窗口控制器实例。该新实例接收来自NIB的
-awakeFromNib
并请求其
窗口
,从而使其加载NIB的另一个实例,并重复该过程

窗口控制器不应在窗口NIB中实例化。NIB中文件的所有者占位符应配置为具有窗口控制器的类。窗口控制器应该在代码中实例化并初始化,以便它使用自己作为NIB的所有者。这将使其填充文件所有者为其保留的位置


此外,您可能应该避免重写
-awakeFromNib
。它可以意外调用。对于那些类型的任务,覆盖
-windowDidLoad
通常更安全。

哦,就是这样。我把控制器对象放在窗口的笔尖上!非常感谢。或者等等,让我再核实一下。。刚刚重新创建了它,现在NIB中没有对象,但是仍然得到它,我会回来的。实际上我仍然有问题。但我仍然从awakeFromNib访问
self.window
,但现在我也删除了它,看起来效果不错
    /* 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;