作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用此代码来配置窗口。之前已经成功了。
- (void)awakeFromNib
{
NSLog(@"self = %p", self);
[(NSPanel *)self.window setWorksWhenModal: NO];
}
无论如何,每次我访问 self.window 时,窗口都会从 Nib 加载。这是一个问题,因为它会递归。但这在其他地方也是一个问题,因为我每次都会得到不同的窗口!
来自“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;
最佳答案
您的窗口 NIB 中有哪些对象?我怀疑您已经在 NIB 中创建了窗口 Controller 类的实例。
因此,每当您加载该 NIB(可能通过您在代码中创建的窗口 Controller 类的实例)时,都会创建窗口 Controller 的新实例。该新实例接收 -awakeFromNib
并请求其窗口
,这会导致它加载 NIB 的另一个实例,并重复该过程。
窗口 Controller 不应在窗口 NIB 中实例化。 NIB 中的文件所有者占位符应配置为具有窗口 Controller 的类。窗口 Controller 应该在代码中实例化并初始化,以便它使用自己作为 NIB 的所有者。这将使它填充文件所有者为其保留的位置。
此外,您应该避免覆盖 -awakeFromNib
。可以说是意料之外的调用。对于此类任务,重写 -windowDidLoad
通常更安全。
关于macos - 为什么每次访问时都会加载我的窗口 `self.window`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42037360/
我是一名优秀的程序员,十分优秀!