3

I want to make NSButton as a 'Default' button so that when i hit "Enter" key that button gets clicked(i.e its Click Action gets called), i am able to do it by setting its key Equivalent to '\r' or by hitting 'Enter' key in Xib.

But in case if the same window does not have title bar, then default button does not work.

I have tried by making myWindow borderless programitcally

[mywindow setStyleMask:NSBorderlessWindowMask];

still it does not help.

So my question is how to make NSButton as 'Default' Button without title bar to its Window.

Thanks.

4

2 回答 2

2

文档

使用 NSBorderlessWindowMask 的窗口不能成为 key 或 main,除非你实现 canBecomeKeyWindow 或 canBecomeMainWindow 返回 YES

子类化NSWindow并实现canBecomeKeyWindowcanBecomeMainWindow制作NSButton为没有标题栏窗口的“默认”按钮。

@interface PBWindow : NSWindow

@end
@implementation PBWindow

-(BOOL)canBecomeKeyWindow
{
    return YES;
}
@end  

将 NSWindow 类名设置为 PBWindow:

在此处输入图像描述

输出:

在此处输入图像描述

于 2013-10-24T11:49:54.297 回答
0

在下面以编程方式尝试: -

NSRect myRect = NSMakeRect(0.0, 0.0, 1, 1);
NSButton *defaultButton = [[NSButton alloc] initWithFrame:myRect];

[defaultButton setKeyEquivalent:@"\r"];
[defaultButton setTarget:self];
[defaultButton setAction:@selector(yourActionMethod:)];
[[self view] addSubview:defaultButton];
[defaultButton release];
于 2013-10-24T09:20:57.900 回答