NSApplication Windows属性-Windows无法删除? [英] NSApplication windows property - windows not removed?
问题描述
我有一个显示模式的NSWindow/Controller.它具有一个类似于这样的动作的关闭"按钮:
I have a NSWindow/Controller that I display modal. It has a "Close" button hooked up to an action like this:
- (IBAction)close:(id)sender
{
[self.window orderOut:sender];
[self.window close];
[[NSApplication sharedApplication] stopModal];
}
在主窗口中,显示模式:
From my main window, I display the modal:
- (IBAction)modal:(id)sender
{
NSLog(@"Before: %lu", [[[NSApplication sharedApplication] windows] count]);
ModalWindowController *modal = [[ModalWindowController alloc] initWithWindowNibName:@"ModalWindowController"];
[[NSApplication sharedApplication] runModalForWindow:modal.window];
NSLog(@"After: %lu", [[[NSApplication sharedApplication] windows] count]);
}
我打开和关闭模态几次,输出如下:
I open and close the modal a few times, and the output is like this:
2013-01-17 14:36:08.071 Modals[3666:303] Before: 1
2013-01-17 14:36:08.962 Modals[3666:303] After: 2
2013-01-17 14:36:09.578 Modals[3666:303] Before: 2
2013-01-17 14:36:11.009 Modals[3666:303] After: 3
2013-01-17 14:36:12.108 Modals[3666:303] Before: 3
2013-01-17 14:36:12.910 Modals[3666:303] After: 4
因此, [[[[NSApplication sharedApplication] windows]计数] 仅增加.
我希望当我打开和关闭模式窗口时,它会增加和减少.我的应用程序使用ARC.有人可以向我解释吗?
I would expect it to increase and decrease as I open and close the modal window. My application uses ARC. Can someone explain this to me?
提前谢谢
推荐答案
您正在关闭窗口,但这并没有取消分配它,因为您的窗口控制器ModalWindowController
仍保留着它.您的样本中没有任何显示窗口控制器被释放的信息.
You're closing your window, but that's not deallocating it because your window controller ModalWindowController
is still retaining it. I don't see anything in your sample to indicate the window controller is being released.
最简单的答案是在调用-runModalForWindow:
之后释放窗口控制器.
The simplest answer to give you is to have you release your window controller following your call to -runModalForWindow:
.
您可能期望的是,当窗口关闭时,窗口控制器将关闭.那是您必须自己实现的事情.在Apple文档的关闭窗口行为"中:
What you may be expecting is the window controller to close when your window does. That's something you have to make happen yourself. From "Window Closing Behavior," in Apple's documentation:
如果要关闭窗口以使窗口和窗口同时显示 控制器不在文档的一部分时消失,您的子类 NSWindowController可以观察NSWindowWillCloseNotification或 窗口委托,实现windowWillClose:方法并包括 您的实现中的以下代码行:"[self autorelease];"
If you want the closing of a window to make both window and window controller go away when it isn’t part of a document, your subclass of NSWindowController can observe NSWindowWillCloseNotification or, as the window delegate, implement the windowWillClose: method and include the following line of code in your implementation: "[self autorelease];"
在您的情况下,这可能不是最好的方法,因为您将有机会调用-stopModal
之前,先丢弃窗口控制器和窗口.
In your scenario, this might not be the best approach, because you'd wind up disposing of both your window controller and window before you get a chance to call -stopModal
.
这篇关于NSApplication Windows属性-Windows无法删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!