Mac 模态窗口关闭事件的处理

来源:互联网 发布:淘宝挂起是什么意思 编辑:程序博客网 时间:2024/05/16 20:08

在Mac 开发中如果使用 runModalForWindow 弹出模态对话框,那么在关闭时需要解除模态状态,否则可能进入一个假死状态。

总结了一下大概可以使用以下几种方式处理窗口window 的关闭事件:

1. 通过创建 MyWindowDelegate : NSObject<NSWindowDelegate> 并且实现方法windowWillClose: 如下:

- (void) windowWillClose:(NSNotification *)notification {[NSApp stopModalWithCode:0];}
这种方法适用SDK 10.6 以上,因为SDK 10.5 还不支持 NSWindowDelegate 。

2. 将窗口关闭消息 NSWindowWillCloseNotification 加入消息中心,代码如下(其中stopAction: 会在窗口关闭时被调用,是需要自己实现的函数。):

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopAction:) name:NSWindowWillCloseNotification object:[self window]];

3.在自己的WindowController 类中直接添加windowShouldClose 方法。

- (BOOL)windowShouldClose:(id)sender {[NSApp stopModalWithCode:1];return TRUE;}

原创粉丝点击