在swift中打开/关闭UISwitch

时间:2018-04-18 15:31:12

标签: swift toggle uiswitch

我有一个问题是打开/关闭UISwitch。

当标有“血糖”的开关打开时,标有“HbA1c”的标记为“HbA1c”的开关关闭。反之亦然。翻转“血糖”,打开“HbA1c”。它工作正常。

但是,使用相同的代码,“HbA1c”UISwitch不会打开/关闭其伴侣“血糖”。我很困惑为什么代码适用于一个UISwitch而不是另一个。

IBAction a1cSwitchPressed已正确连接,我在功能中粘贴了标签更新。

uiswitch toggle

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

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

1 个答案:

答案 0 :(得分:0)

Because of copy-paste the usual technique we always use to repeat the same item , a IB connection to a UI component is copied also (IBOutlet,IBAction) which leads to unexpected results because it misses the connection to the actual item we thought it connected to