在 swift 中切换 UISwitch on/off

Toggle UISwitch on/off in swift

我在切换 UISwitch 时遇到问题 on/off。

当标记为 "blood glucose" 的开关打开时,其下方标记为 "HbA1c" 的开关将关闭。反之亦然。翻转"blood glucose"关闭,"HbA1c"打开。它工作正常。

但是,使用相同的代码,"HbA1c" UISwitch 不会切换其合作伙伴 "blood glucose" on/off。我很困惑为什么代码适用于一个 UISwitch 而不是另一个。

IBAction a1cSwitchPressed 已正确连接,我在起作用的函数中插入了标签更新。

@IBAction func eagSwitchPressed(_ sender: UISwitch) {
 a1cSwitch.setOn(!sender.isOn, animated: true)     
}

@IBAction func a1cSwitchPressed(_ sender: UISwitch) {
 eagSwitch.setOn(!sender.isOn, animated: true)
}

由于复制粘贴是我们经常使用的重复相同项目的常用技术,与 UI 组件的 IB 连接也被复制 (IBOutlet,IBAction)导致意想不到的结果,因为它错过了与我们认为它连接的实际项目的连接