作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Safari 浏览器插件,我想在其中打开一个 NSWindow 来显示版权说明。虽然对话框中的“确定”按钮关闭了对话框的窗口,并且运行良好,但当我单击左上角的红色关闭窗口“x”时,它也关闭了窗口,但它是父窗口(整个浏览器选项卡,我插件在运行),仍然处于禁用状态,就好像模态窗口在某处打开一样。
我什至尝试将一个新的选择器附加到窗口关闭通知,它运行与确定按钮相同的代码,但仍然无法正常工作。
这是代码的相关部分:
- (void) closeBox
{
// called when the Ok button pressed
[NSApp abortModal];
}
- (void)closeClicked:(NSNotification *)notification
{
// called when the close window 'x' button pressed
NSLog(@"Closed");
[NSApp abortModal];
}
- (void) openBox
{
NSRect frame = NSMakeRect(0, 0, 300, 250);
mwin = [[[NSWindow alloc] initWithContentRect:frame
styleMask:NSClosableWindowMask |NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO] autorelease];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(closeClicked:)
name:NSWindowWillCloseNotification
object:mwin];
NSButton * bn;
bn = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 100, 20) ] autorelease];
[bn setButtonType:NSMomentaryPushInButton];
[bn setTitle:@"Ok"];
[bn setTarget:self];
[bn setAction:@selector(closeBox)];
[[mwin contentView] addSubview:bn];
[NSApp runModalForWindow:mwin];
}
最佳答案
我已经修改了您的代码,尝试如下所示:-
- (void) closeBox
{
// called when the Ok button pressed
//Commented this line
// [NSApp abortModal];
[mwin performClose:mwin];// Modified this line
}
//Modified below notification just comment the parameter
- (void)closeClicked/*:(NSNotification *)notification*/
{
[NSApp abortModal];
}
- (void) openBox
{
NSRect frame = NSMakeRect(0, 0, 300, 250);
mwin = [[[[NSWindow alloc] initWithContentRect:frame
styleMask:NSClosableWindowMask |NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:NO]retain]autorelease];
//Modified notification below
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(closeClicked)
name:NSWindowWillCloseNotification
object:nil];
NSButton * bn;
bn = [[[NSButton alloc] initWithFrame:NSMakeRect(10, 10, 100, 20) ] autorelease];
[bn setButtonType:NSMomentaryPushInButton];
[bn setTitle:@"Ok"];
[bn setTarget:self];
[bn setAction:@selector(closeBox)];
[[mwin contentView] addSubview:bn];
[NSApp runModalForWindow:mwin];
}
关于objective-c - NSClosableWindowMask 对话框行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20491913/
我有一个 Safari 浏览器插件,我想在其中打开一个 NSWindow 来显示版权说明。虽然对话框中的“确定”按钮关闭了对话框的窗口,并且运行良好,但当我单击左上角的红色关闭窗口“x”时,它也关闭了
我是一名优秀的程序员,十分优秀!