performClose:在无边框窗口上?

时间:2011-02-19 15:01:22

标签: objective-c cocoa xcode nswindow

有没有办法让方法执行关闭:当我使用NSBorderlessWindowMask时工作?我必须覆盖什么方法? (如果可能的话)

3 个答案:

答案 0 :(得分:10)

我已经覆盖了一些方法,并且它有效。我现在想知道的是,如果我没有从糟糕的方法中接受它:)

- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
{
    return ([menuItem action] == @selector(performClose:) || [menuItem action] == @selector(performZoom:) || [menuItem action] == @selector(performMiniaturize:)) ? YES : [super validateMenuItem:menuItem];
}

- (BOOL)windowShouldClose:(id)sender
{
    return YES;
}

- (void)performClose:(id)sender
{
    if([[self delegate] respondsToSelector:@selector(windowShouldClose:)])
    {
        if(![[self delegate] windowShouldClose:self]) return;
    }
    else if([self respondsToSelector:@selector(windowShouldClose:)])
    {
        if(![self windowShouldClose:self]) return;
    }

    [self close];
}

- (void)performMiniaturize:(id)sender
{
    [self miniaturize:self];
}

- (void)performZoom:(id)sender
{
    [self zoom:self];
}

答案 1 :(得分:1)

由于无边框窗口没有关闭按钮,performClose:将无效。您应该使用close方法。但是,此方法也不会在窗口的委托上调用windowShouldClose:,也不能挂钩到IBAction。如果您需要这些功能,可以创建一个包装器方法来处理关闭。

@interface NSWindow (BorderlessClosing)
- (IBAction)performBorderlessClose:(id)sender;
@end

@implementation NSWindow (BorderlessClosing)
- (IBAction)performBorderlessClose:(id)sender {
    if([[self delegate] respondsToSelector:@selector(windowShouldClose:)]) {
        if(![[self delegate] windowShouldClose:self]) return;
    } else if([self respondsToSelector:@selector(windowShouldClose:)]) {
        if(![self windowShouldClose:self]) return;
    }
    [self close];
}
@end

答案 2 :(得分:1)

正如凯尔所说,覆盖你的窗口并添加

-(BOOL)canBecomeKeyWindow
{
    return YES;
}

在你的WindowController中,

 - (IBAction)performClose:(id)sender
使用Close MenuItem时应该调用