小编chr*_*ris的帖子

以编程方式向 NSWindow 添加关闭按钮

我想以NSWindow编程方式向一个关闭按钮添加一个关闭按钮。我可以让按钮显示,但没有鼠标悬停或鼠标按下效果。当我单击按钮时,我的“选择器”似乎永远不会被调用。我不太确定出了什么问题,为什么这很烦人。

这是我一直在搞乱的:

closeButton = [NSWindow standardWindowButton:NSWindowCloseButton forStyleMask:self.styleMask];

NSView *themeFrame = [[self contentView] superview];
NSRect c = [themeFrame frame];  // c for "container"
NSRect aV = [closeButton frame];    // aV for "accessory view"
NSRect newFrame = NSMakeRect(                                                c.size.width - aV.size.width - 5,  // x position                                                c.size.height - aV.size.height - 5,    // y position                                           aV.size.width,  // width                                                aV.size.height); // height

[closeButton setFrame:newFrame];    
[themeFrame addSubview:closeButton];
[closeButton setAutoresizingMask:NSViewMaxXMargin | NSViewMinYMargin];  
[closeButton setEnabled:YES];
[closeButton setTarget:self];
[closeButton setAction:NSSelectorFromString(@"testClick:") ];
Run Code Online (Sandbox Code Playgroud)

其中“testClick”只是我班级的成员函数,定义如下:

- (void)testClick:(id)sender
Run Code Online (Sandbox Code Playgroud)

问题似乎是调用:
[themeFrame …

cocoa button nswindow

5
推荐指数
1
解决办法
4017
查看次数

标签 统计

button ×1

cocoa ×1

nswindow ×1