如何在不单击的情况下更改 swift 中复选框的状态?
How can i change the State of a checkbox in swift without clicking it?
例如,我有两个复选框,我的目标是如果复选框 1 被选中,那么复选框 2 也被选中。如果复选框 1 未选中,则复选框 2 也未选中。如何在不单击复选框的情况下更改复选框的状态?
在 UISwitch
class:
中有一个方法
func setOn(Bool, animated: Bool)
示例:
yourSwitch.setOn(true, animated: true)
为了 link twi 相互切换,您必须编写更复杂的东西。如果您在故事板中创建视图,代码看起来会有点像这样。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var firstSwitch: UISwitch!
@IBOutlet weak var secondSwitch: UISwitch!
@IBAction func firstValueChanged() {
secondSwitch.setOn(firstSwitch.isOn, animated: true)
}
@IBAction func secondValueChanged() {
firstSwitch.setOn(secondSwitch.isOn, animated: true)
}
}
像这样将您的复选框与视图控制器连接起来:
class ViewController: UIViewController {
@IBOutlet weak var checkBox1: UISwitch!
@IBOutlet weak var checkBox2: UISwitch!
}
然后将第一个复选框的 IBAction 添加到视图控制器,同时使用 if else 来切换第二个复选框,如下所示:
class ViewController: UIViewController {
@IBOutlet weak var checkBox1: UISwitch!
@IBOutlet weak var checkBox2: UISwitch!
@IBAction func checkBox1Pressed(_ sender: UISwitch) {
// using if else
if checkBox1.isOn {
checkBox2.setOn(true, animated: true)
} else {
checkBox2.setOn(false, animated: true)
}
}
}
或使用三元运算符,例如:
class ViewController: UIViewController {
@IBOutlet weak var checkBox1: UISwitch!
@IBOutlet weak var checkBox2: UISwitch!
@IBAction func checkBox1Pressed(_ sender: UISwitch) {
// or using ternary operator
checkBox1.isOn ? checkBox2.setOn(true, animated: true) : checkBox2.setOn(false, animated: true)
}
}
结果:
如果您不使用 UISwitch,请使用您当前的代码更新您的问题
根据 OP 的评论更新:
像这样将您的复选框与视图控制器连接起来:
class ViewController: NSViewController {
@IBOutlet weak var checkBox1: NSButton!
@IBOutlet weak var checkBox2: NSButton!
}
然后将第一个复选框的 IBAction 添加到视图控制器,同时使用 if else 来切换第二个复选框,如下所示:
class ViewController: NSViewController {
@IBOutlet weak var checkBox1: NSButton!
@IBOutlet weak var checkBox2: NSButton!
@IBAction func checkBox1Pressed(_ sender: NSButton) {
// Note: state checked == 1, state unchecked == 0
// if checkBox1 is checked
if checkBox1.state == 1 {
// also set checkBox2 on checked state
checkBox2.state = 1
} else {
// uncheck checkBox2
checkBox2.state = 0
}
}
}
或使用三元运算符,例如:
class ViewController: NSViewController {
@IBOutlet weak var checkBox1: NSButton!
@IBOutlet weak var checkBox2: NSButton!
@IBAction func checkBox1Pressed(_ sender: NSButton) {
// Note: state checked == 1, state unchecked == 0
// or using ternary operator
checkBox1.state == 1 ? (checkBox2.state = 1) : (checkBox2.state = 0)
}
}
结果:
例如,我有两个复选框,我的目标是如果复选框 1 被选中,那么复选框 2 也被选中。如果复选框 1 未选中,则复选框 2 也未选中。如何在不单击复选框的情况下更改复选框的状态?
在 UISwitch
class:
func setOn(Bool, animated: Bool)
示例:
yourSwitch.setOn(true, animated: true)
为了 link twi 相互切换,您必须编写更复杂的东西。如果您在故事板中创建视图,代码看起来会有点像这样。
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var firstSwitch: UISwitch!
@IBOutlet weak var secondSwitch: UISwitch!
@IBAction func firstValueChanged() {
secondSwitch.setOn(firstSwitch.isOn, animated: true)
}
@IBAction func secondValueChanged() {
firstSwitch.setOn(secondSwitch.isOn, animated: true)
}
}
像这样将您的复选框与视图控制器连接起来:
class ViewController: UIViewController {
@IBOutlet weak var checkBox1: UISwitch!
@IBOutlet weak var checkBox2: UISwitch!
}
然后将第一个复选框的 IBAction 添加到视图控制器,同时使用 if else 来切换第二个复选框,如下所示:
class ViewController: UIViewController {
@IBOutlet weak var checkBox1: UISwitch!
@IBOutlet weak var checkBox2: UISwitch!
@IBAction func checkBox1Pressed(_ sender: UISwitch) {
// using if else
if checkBox1.isOn {
checkBox2.setOn(true, animated: true)
} else {
checkBox2.setOn(false, animated: true)
}
}
}
或使用三元运算符,例如:
class ViewController: UIViewController {
@IBOutlet weak var checkBox1: UISwitch!
@IBOutlet weak var checkBox2: UISwitch!
@IBAction func checkBox1Pressed(_ sender: UISwitch) {
// or using ternary operator
checkBox1.isOn ? checkBox2.setOn(true, animated: true) : checkBox2.setOn(false, animated: true)
}
}
结果:
如果您不使用 UISwitch,请使用您当前的代码更新您的问题
根据 OP 的评论更新:
像这样将您的复选框与视图控制器连接起来:
class ViewController: NSViewController {
@IBOutlet weak var checkBox1: NSButton!
@IBOutlet weak var checkBox2: NSButton!
}
然后将第一个复选框的 IBAction 添加到视图控制器,同时使用 if else 来切换第二个复选框,如下所示:
class ViewController: NSViewController {
@IBOutlet weak var checkBox1: NSButton!
@IBOutlet weak var checkBox2: NSButton!
@IBAction func checkBox1Pressed(_ sender: NSButton) {
// Note: state checked == 1, state unchecked == 0
// if checkBox1 is checked
if checkBox1.state == 1 {
// also set checkBox2 on checked state
checkBox2.state = 1
} else {
// uncheck checkBox2
checkBox2.state = 0
}
}
}
或使用三元运算符,例如:
class ViewController: NSViewController {
@IBOutlet weak var checkBox1: NSButton!
@IBOutlet weak var checkBox2: NSButton!
@IBAction func checkBox1Pressed(_ sender: NSButton) {
// Note: state checked == 1, state unchecked == 0
// or using ternary operator
checkBox1.state == 1 ? (checkBox2.state = 1) : (checkBox2.state = 0)
}
}
结果: