swift - 如何在 OSX、Xcode 8、Swift 3 中实现单选按钮?

标签 swift xcode macos swift3

我正在尝试在 OSX、Xcode 8、Swift 3 中实现单选按钮。我可以在过去的 Xcode 迭代中使用单选按钮组来实现这一点。我相信应该有一种方法可以在 Xcode 中对单选按钮进行分组,以便它们的选择是排他性的,但我不知道如何实现。有人可以解释一下现在该怎么做吗?

Apple documentation解释说

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).

...但我无法在 Interface Builder 中弄清楚如何为每个 NSButton 实例分配相同的操作。我从第二个按钮拖动到 Xcode IDE 中的一个 Action ,它似乎无法选择相同的 Action 。有人可以解释正确的过程吗?

非常感谢!

最佳答案

看起来 IDE 中可能存在错误 — 如果操作具有 Any 类型的参数,它会感到困惑。您可以通过修改操作方法以显式接受 NSButton 发送者来解决它:

demo gif

关于swift - 如何在 OSX、Xcode 8、Swift 3 中实现单选按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41539229/

相关文章:

ios - 在 OSX 和 IOS 应用程序之间共享代码的最佳实践

python - 警告 : `pyenv init -` no longer sets PATH. MacOS

ios - 如何通过制作单词列表文件的数组来确定数组中的每个元素是否包含某个字母(swift 2)

swift - Push Segues、导航 Controller 和工具/导航栏之间的关系

ios - Xcode 收到非 HTTP 200 响应 (400)

ios - 返回 UITableView 的函数会导致 EXC_BAD_ACCESS

ios - 如何将 SCNView 更改回 UIView?

objective-c - 将 Cocoa 应用程序和命令行工具合并到一个应用程序包中

ios - 快速对齐 TextView 中的文本

ios - 需要找到用户手动拖动或自动聚焦改变的mapView区域