【问题标题】:How do I check the state of a checkbox for macOS applications using Swift如何使用 Swift 检查 macOS 应用程序的复选框状态
【发布时间】:2015-01-09 23:40:04
【问题描述】:

这个任务看起来比较简单,我想要一个 if 语句来确定一个复选框是否被选中,或者没有明显的复选框,但不知所措

我试过了

如果 checkbox.state == everythign 我能想到的,但它总是错误 EXC_BAD_INSTRUCTION 或使用错误的无法将变量类型转换为其他变量类型

【问题讨论】:

    标签: swift xcode macos checkbox nsbutton


    【解决方案1】:

    Xcode 9 • Swift 4

    您可以切换复选框状态属性以检查它是打开还是关闭,如下所示:

    switch sender.state {
    case .on:
        print("on")
    case .off:
        print("off")
    case .mixed:
        print("mixed")
    default: break
    }
    

    【讨论】:

    • 你应该使用枚举常量 NSOnState 和 NSOffState
    • 附注:else 部分涵盖 NSOffState 和 NSMixedState。
    【解决方案2】:

    我在我的项目上做了这个,效果很好

    if sender.state == .on{
                casa_ospiti_text.stringValue = "CASA"
            }
            else{
                casa_ospiti_text.stringValue = "OSPITI"
            }
    

    它使用常量字符串更改文本字段 (casa_ospiti_text) 的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-25
      相关资源
      最近更新 更多