我有一个Safari浏览器插件,在其中,我想打开一个NSWindow来显示版权说明。当对话框中的Ok按钮关闭对话框的窗口,并且工作正常时,当我点击左上角的红色关闭窗口“x”时,它也会关闭窗口,但它是父窗口(整个浏览器选项卡,我让插件运行),保持禁用状态,好像某个模态窗口仍在某处打开。
我甚至试图在窗口关闭通知中附加一个新的选择器,它会运行与Ok按钮相同的代码,但仍然无法正常工作。
以下是代码的相关部分:
- (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];
}
答案 0 :(得分:1)
我修改了你的代码,如下所示: -
- (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];
}
答案 1 :(得分:0)
经过几个小时的谷歌搜索,我发现其他人有类似的问题,解决方案很简单:窗口对象由NSNotificationCenter的观察者保留。我所要做的就是删除观察者:
- (void) closeClicked:(NSNotification *)notification
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[NSApp abortModal];
NSLog(@"Closed");
}
现在它正常运作。