NSApplication windows 属性 - 窗口未删除? [英] NSApplication windows property - windows not removed?

查看:26
本文介绍了NSApplication windows 属性 - 窗口未删除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个显示模态的 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 属性 - 窗口未删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆