ios - 防止 UISwitch 改变 UI 状态

标签 ios swift return uiswitch

我有一个执行操作的 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/

相关文章:

ios - Razorpay 没有重定向到结帐页面,为什么?

java - 返回 HashSet 中对象的连接某些字段的字符串

java - 如何从同一个java方法返回两个多维数组?

ios - LibGDX/支持iOS(RoboVM)/Android Studio的Admob

ios - 试图将数组传递给 swift 函数

arrays - 获取字典数组中的重复值。 swift

swift - 在 Swift 中检查密码字符串强度标准

python - 从变量打印原始字符串? (没有得到答案)

iphone - glDrawArrays 在每一帧上分配内存

ios - 使用 dequeueReusableCellWithIdentifier 时无法显示 detailTextLabel