ios - 如何使用 alert 来确认或取消 View 转换?

标签 ios swift uialertcontroller uialertaction

在某些情况下,如果用户打算离开当前 View (例如弹出当前 View 、推送其他 View 或选择其他选项卡项等),则应显示 UIAlertController 以确认用户的真实意图。

用户可以按 OK 继续 View 转换,或按 Cancel 留在当前 View 。

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)
    if someConditions {
        promptUIAlert()
    }
}

有什么解决方案可以达到这个要求吗?

最佳答案

首先,您不能处理viewWillDisappear,因为此时已经确定 View 将消失。无论您有 View 转换(推送、弹出、呈现、关闭),您都需要处理此问题。

您必须处理确认警报操作中的转换。您的警报看起来像这样。

let alert = UIAlertController(title: "Wait!", message: "Are you sure you want to leave this view?", preferredStyle: .alert)
let ok = UIAlertAction(title: "OK", style: .default) { (alertAction) in
    //Handle view transitioning here
}
let cancel = UIAlertAction(title: "Cancel", style: .destructive) { (alertAction) in
    //Do nothing?
}
alert.addAction(ok)
alert.addAction(cancel)
present(alert, animated: true, completion: nil)

关于ios - 如何使用 alert 来确认或取消 View 转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52494515/

相关文章:

ios - 如何在应用程序委托(delegate) (obj-c) 中添加 UIAlertController

ios - 只允许一个 UIAlertController View 从可见 View 上的类中呈现

iphone - 本地通知计划问题

ios - 谷歌地图SDK : Undefined symbols for architecture x86_64

ios - 更新 RLMObject 为 Swift/IOS

ios - 创建一个空的 var 给我一个错误

multithreading - OS X GCD 多线程并发使用更多 CPU 但执行速度比单线程慢

ios - 我有错误 : no visible interface for 'UIAlertController'

ios - 如何使用 StoreKit 更新订阅?

ios - SpriteKit 中的瓷砖游戏 map 存储