objective-c - NSPushOnPushOffButton 不停留 "Pushed In"

标签 objective-c cocoa nsbutton nsbuttoncell

我正在尝试以编程方式向 View 添加一个按钮,我希望它在单击时保持“插入”状态,直到再次单击它。从我读过的内容来看,用于此目的的按钮似乎是 NSPushOnPushOffButton。这是 Apple 对其的描述:

The first click both highlights and causes the button to be “pushed in” if the button is bordered; a second click returns it to its normal state. This option is called “Push On Push Off” in Interface Builder’s Button Inspector.

它在我的程序中不起作用,所以我创建了一个 SSCCE,我将在下面包含它。我的问题是单击时按钮没有停留,而是像普通按钮一样弹出。我不确定我是否遗漏了有关此按钮应该如何执行的某些信息,或者我是否只是错误地实现了它(这是我第一次尝试以编程方式执行 UI),但其他按钮类型如因为 NSToggleButton 工作,所以除非 NSPushOnPushOffButton 有什么特别之处,否则我看不出哪里出错了。如果有人可以提出更好的解决方案或解释我哪里出错了,那就太好了。谢谢!

#import "AppDelegate.h"

@implementation AppDelegate

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSRect buttonFrame = NSMakeRect(0,0,200,200);
    NSButton *button = [[NSButton alloc]initWithFrame:buttonFrame];
    [self.window.contentView addSubview:button];
    [button setButtonType:NSPushOnPushOffButton];
}

@end

最佳答案

看来你必须设置一个“bezel style”,例如

[button setBezelStyle:NSRegularSquareBezelStyle];

将其添加到您的示例代码后,它按预期工作。

关于objective-c - NSPushOnPushOffButton 不停留 "Pushed In",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17506712/

相关文章:

multithreading - “Autoreleased with no pool in place”是什么意思?

iphone - 核心数据: Is it possible to build a desktop app to create the data model for an iPhone app with Core Data?

cocoa - NSTextField 添加取消按钮

objective-c - Cocoa:如何以模式方式显示窗口?

iphone - 以编程方式添加 TabBarController

objective-c - 私有(private)@property 是否创建@private 实例变量?

objective-c - 当光标悬停在 NSTextView 内的 NSButton 上时,如何强制光标为 "arrowCursor"?

objective-c - 如何从字符串格式化日期时间

iOS CollectionView 'NSInternalInconsistencyException'

macos - (NSWindowButton) NSWindow.standardWindowButton 按钮未突出显示