尝试识别发件人时缺少单选按钮标识符

Missing Identifier for Radio Button when trying to identify the Sender

我正在尝试使用界面生成器标识符来识别单击的单选按钮。

 @IBAction func text_radio_changed(_ sender: Any) {
        let button:NSButton = sender as! NSButton
        let id:String = button.accessibilityIdentifier()
        print("===========>"+id)
    }

但是控制台一直在打印这个

===========>

没有标识符

在您的代码中,您实际上是在尝试访问可访问性标识符,这是另一回事。要识别单选按钮,您应该使用标签。为按钮设置一个标签,然后像这样读取它。

@IBAction func text_radio_changed(_ sender: Any) {
    let button:NSButton = sender as! NSButton
    let id:Int = button.tag
    print("===========>"+id)
}

注意:您实际上可以使用辅助功能 ID 来做同样的事情。检查其他类似的 post.

Update:"Willeke"的答案似乎更好(如果有效),我习惯于为iOS开发和我不知道 NSButtons 有一个标识符 属性。

button.accessibilityIdentifier 是辅助功能标识。身份标识符是 button.identifier.

@IBAction func text_radio_changed(_ sender: Any) {
    let button:NSButton = sender as! NSButton
    let id:String = button.identifier!.rawValue
    print("===========>"+id)
}