在某些情况下,如果用户打算离开当前 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/