How to close a window (unload a NIB)?

跟風遠走 提交于 2019-12-10 10:15:45

问题


I have a custom NSWindowController subclass that loads a NIB file during initialization like this:

self = [super initWithNibNamed:@"myNib"];
if (self != nil) {
    [self window];
}

The nib contains some custom views and some other controls. The NSWindowController is the file's owner and at least one of the views even binds to it.

Simply, what do I have to do to close and release that window? I spend the whole day trying to figure that out and I am still clueless.


回答1:


You don't unload a nib; “loading” it is simply unarchiving the objects that are archived within it. That's not a state that persists indefinitely; it's a momentary action. Once you've unarchived the object, it doesn't matter where it came from.

If you were not in a window controller, then:

  • To close a window, you would send it a close message.
  • To close and release a window, you would need to either have its releasedWhenClosed property turned on (you can do this in IB) before you send it a close message, or send it a release message after the close message.

But since you are in a window controller, just send yourself a close message.

See also “Window Closing Behavior” in the Document-Based Applications Overview (document-based apps being the main users of window controllers).




回答2:


Thanks @peter hosey.

[self close]; // will work only if your nib is wired up correctly.

Open the nib for your file. On the Window you need to wire the "Referencing Outlet" of the window to the "Files Owner" and choose Window. Otherwise nothing will work.



来源:https://stackoverflow.com/questions/2648536/how-to-close-a-window-unload-a-nib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!