我有一个执行操作的 UISwitch
。我需要显示带有是/否选项的警报。如果用户选择选项No,则不会发生任何事情。开关的 UI 不应更改。
这是我的代码。
func toggleSwitchValueChanged(sender: UISwitch) {
let alert = UIAlertController(title: "", message: "This operation will reset the photos you have renamed already. Proceed?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "No", style: .Cancel) { action in
return
})
alert.addAction(UIAlertAction(title: "Yes", style: .Default) { action in
self.api.includeCoverPhoto(sender.on)
})
alert.preferredAction = alert.actions[1]
presentViewController(alert, animated: true, completion: nil)
}
我在 UISwitch
的值更改事件上触发此方法。
问题是如果我选择否,操作不会继续。但是 UISwitch
的 UI 变成了另一种状态。
如何阻止这种情况发生?
最佳答案
func toggleSwitchValueChanged(sender: UISwitch) {
let alert = UIAlertController(title: "", message: "This operation will reset the photos you have renamed already. Proceed?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "No", style: .Cancel) { action in
sender.setOn(true, animated:false)
return
})
....
关于ios - 防止 UISwitch 改变 UI 状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35771156/