xcode 如何使用 Swift 检查 macOS 应用程序的复选框状态

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27871058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:28:12  来源:igfitidea点击:

How do I check the state of a checkbox for macOS applications using Swift

swiftxcodemacoscheckboxnsbutton

提问by Jason M

The task seemed relatively simple, I wanted an if statement to determine if a check box is checked or not apparently check boxes but am at a loss

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

I've tried

我试过了

if checkbox.state == everythign i can think of but it always errors either EXC_BAD_INSTRUCTION or using wrong cant convert variable type to other variable type

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

回答by Leo Dabus

Xcode 9 ? Swift 4

Xcode 9?斯威夫特 4

You can switch the checkbox state property to check if it is on or off as follow:

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

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

回答by Giacomo Sighinolfi

i did this on my project and it worked well

我在我的项目中做了这个并且效果很好

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

where it changes the value of a textfield (casa_ospiti_text) with constants strings.

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