标签:oid shu upload sele blank types ram serial textfield
NSButtonTypeSwitch 就是勾选样式
其他样式可参考:http://blog.csdn.net/lovechris00/article/details/77976480
allowsMixedState 代表是否可以混合选择。YES-有三种状态,-1、1、0;NO-2种状态,1、0。
使用 setAction 来监听。
只设置 buttonType 即可,不用设置 bezielType,设置了也无效。存疑: 这两者如何组合使用?
- (void)addCheckBtn{
NSButton *btn0 = [[NSButton alloc]init];
btn0.frame = NSMakeRect(100, 100, 100, 100);
btn0.wantsLayer = YES;
btn0.layer.backgroundColor = [NSColor cyanColor].CGColor;
[btn0 setButtonType:NSButtonTypeSwitch];
//YES-有三种状态,-1、1、0
//NO-2种状态,1、0
btn0.allowsMixedState = YES;
[self.window.contentView addSubview:btn0];
[btn0 setAction:@selector(valueChange:)];
}
- (void)valueChange:(NSButton *)sender{
NSButton *checkBtn = sender;
BOOL isOn = checkBtn.state;
NSLog(@" %d",isOn);
}
---------------------
作者:lovechris00
来源:CSDN
原文:https://blog.csdn.net/lovechris00/article/details/77977824
版权声明:本文为博主原创文章,转载请附上博文链接!
NSButtonType
typedef NS_ENUM(NSUInteger, NSButtonType) {
NSButtonTypeMomentaryLight = 0,
NSButtonTypePushOnPushOff = 1,
NSButtonTypeToggle = 2,
NSButtonTypeSwitch = 3,
NSButtonTypeRadio = 4,
NSButtonTypeMomentaryChange = 5,
NSButtonTypeOnOff = 6,
NSButtonTypeMomentaryPushIn = 7,
NSButtonTypeAccelerator NS_ENUM_AVAILABLE_MAC(10_10_3) = 8,
NSButtonTypeMultiLevelAccelerator NS_ENUM_AVAILABLE_MAC(10_10_3) = 9,
};
显示结果
小结:
- 1、6 类型(NSButtonTypePushOnPushOff、NSButtonTypeOnOff),选中状态是蓝色,再次点击才会恢复到原来的颜色。
- 0、2、7(NSButtonTypeMomentaryLight、NSButtonTypeToggle、NSButtonTypeMomentaryPushIn) 点击时会有背景色(高亮状态)。
- 5(NSButtonTypeMomentaryChange) 点击时会有高亮状态,文字一闪,但是没有高亮的背景色。
- 3(NSButtonTypeSwitch) 适合做多选
- 4 (NSButtonTypeRadio) 适合做单选。
测试代码
- (void)addSerialBtn2{
CGFloat btnW = 80;
CGFloat btnH = 40;
for (int i = 0; i < 8; i++) {
NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect( 20 + (i % 5) * (btnW + 5) ,50 + (i / 5) * (btnH + 40), btnW, btnH)];
// btn.bezelStyle = i;
btn.bezelStyle = NSRoundedBezelStyle;
[btn setButtonType:i];
NSString *btnName = [NSString stringWithFormat:@"按钮 - %d",i];
[btn setTitle:@"按钮"];
// [btn setTitle:@""];
btn.wantsLayer = YES;
btn.layer.backgroundColor = [NSColor cyanColor].CGColor;
[self.window.contentView addSubview:btn];
NSTextField *field = [[NSTextField alloc]initWithFrame:NSMakeRect(CGRectGetMinX(btn.frame), CGRectGetMinY(btn.frame) - 22, btnW, 20)];
field.stringValue = btnName;
field.bezelStyle = i;
[self.window.contentView addSubview:field];
}
}
---------------------
MacOS 开发 - NSButton - 勾选框(NSButtonTypeSwitch)
标签:oid shu upload sele blank types ram serial textfield
原文地址:https://www.cnblogs.com/sundaymac/p/10334170.html