performClose: on a borderless window?
Is there a way开发者_运维知识库 to have the method performClose: working when i use NSBorderlessWindowMask? What method do i have to override ? (if its possible)
i have overridden some methods, and it works. All I want to know now is if i didn't take it from the bad approach :)
- (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];
}
Since a borderless window doesn't have a close button, performClose:
will not work. You should instead use the close
method. However, this method also does not call windowShouldClose:
on the window's delegate and cannot be hooked to an IBAction. If you want these features, you could create a wrapper method to handle closing for you.
@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
As Kyle said, override your window and and add
-(BOOL)canBecomeKeyWindow
{
return YES;
}
And in your WindowController,
- (IBAction)performClose:(id)sender
should be called when using Close MenuItem.
精彩评论