gpt4 book ai didi

swift - NSButton RadioGroup(NSMatrix 替代方案)

转载 作者:IT王子 更新时间:2023-10-29 05:36:26 36 4
gpt4 key购买 nike

我已经尝试了几次来设置几个类似的按钮,它们都连接到相同的 IBAction,但似乎仍然无法复制 RadioButton 的行为。

目前,我有 5 个按钮,一个 NSView 的所有子项..

NSView
- ButtonOne - NSButton (Square Button)
- ButtonTwo - NSButton (Square Button)
- ButtonThree - NSButton (Square Button)
- ButtonFour - NSButton (Square Button)
- ButtonFive - NSButton (Square Button)

它们都连接到一个通用的 IBAction,其内容如下:

@IBAction func activityButtonPressed(sender: NSButton) {

println("\(sender.title) Pressed")

}

它的工作原理是捕获实际的 mosueDown 事件并将其发送到 func(如预期的那样)..

如何让它们在单选按钮模式下工作?也就是说,当单击各种按钮时,它们的 NSOnState 和 NSOffState 会切换吗?

当我尝试将按钮类型更改为单选按钮时,它会自动翻转回“切换”......

干杯,

一个

最佳答案

只需给 NSRadioButtons 相同的 super View 和操作。正如 Good Doug 在他的回答中指出的那样,

[…] if you are using square button style, you can't use radio type. You want to use the On Off type in Interface Builder.

我真的不认为你想让你的操作方法设置组中所有按钮的状态......让 AppKit 为你做。

来自 OS X Developer Release Notes: Cocoa Application Framework (10.10 and Earlier)

An NSButton configured as a radio button (with the -buttonType set to NSRadioButton), will now operate in a radio button group for applications linked on 10.8 and later. To have the button work in a radio group, use the same -action for each NSButton instance, and have the same superview for each button. When these conditions are met, checking one button (by changing the -state to 1), will uncheck all other buttons (by setting their -state to 0).

关于swift - NSButton RadioGroup(NSMatrix 替代方案),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30530326/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com