警报框未显示。 X 代码是跳过 Swift 中的警报框代码
Posted
技术标签:
【中文标题】警报框未显示。 X 代码是跳过 Swift 中的警报框代码【英文标题】:Alert Box Is not showing. X Code is Skip the alert box code in Swift 【发布时间】:2021-08-13 04:43:18 【问题描述】:当我在ViewController2
中performSegue
然后在ViewController1
中自动调用unwindToMyHours
。
那么我不知道为什么 X Code 会跳过呈现Alert BOX
的代码。
OUTPUT
-> 其他部分完成。
和 xCode 跳过 -> Task 1
和 task 2
~ViewController2的代码
@IBAction func CancelButtonTapped(_ sender: UIButton)
let alert = UIAlertController(title: "Delete Time", message: "Are you sure?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: [self] _ in
self.deleted = true
self.performSegue(withIdentifier: "unwindToMyHours", sender: nil)
))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
~ViewController1的代码
@IBAction func unwindToMyHours( _ seg: UIStoryboardSegue)
if (seg.source as? ViewController2)?.submitted == true
loadData()
else if (seg.source as? ViewController2)?.deleted == true
let alert = UIAlertController(title: "Deleted", message: "Tabel have been updated", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: alert in
print("Task 1")
))
present(alert, animated: true)
print("Task 2")
print("Else part complete")
【问题讨论】:
【参考方案1】:嗯。 也许您忘记销毁您的第一个警报控制器。
在不破坏第一个警报控制器的情况下,您呈现第二个警报控制器,因此 XCode 无法呈现您的第二个警报控制器。
你的代码看起来不错……让我编辑一下。
~ViewController2的代码
@IBAction func CancelButtonTapped(_ sender: UIButton)
let alert = UIAlertController(title: "Delete Time", message: "Are you sure?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Delete", style: .destructive, handler: [self] _ in
self.deleted = true
self.dismiss(animated: false, completion: nil) // You are forgetting this
self.performSegue(withIdentifier: "unwindToMyHours", sender: nil)
))
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
~ViewController1 的代码 -> 相同的代码
@IBAction func unwindToMyHours( _ seg: UIStoryboardSegue)
if (seg.source as? ViewController2)?.submitted == true
loadData()
else if (seg.source as? ViewController2)?.deleted == true
let alert = UIAlertController(title: "Deleted", message: "Tabel have been updated", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: alert in
print("Task 1")
))
present(alert, animated: true)
print("Task 2")
print("Else part complete")
【讨论】:
【参考方案2】:这很可能是因为您之前的视图控制器还没有被解雇,并且您不能present
一个新的视图控制器(警报是),直到您的视图控制器成为顶部。等到发生这种情况:
var shouldDisplayDeletedAlert = false
override func viewDidAppear(_ animated: Bool)
super.viewDidAppear(animated)
if shouldDisplayDeletedAlert
let alert = UIAlertController(title: "Deleted", message: "Tabel have been updated", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: alert in
print("Task 1")
))
present(alert, animated: true)
print("Task 2")
shouldDisplayDeletedAlert = false
@IBAction func unwindToMyHours( _ seg: UIStoryboardSegue)
if (seg.source as? ViewController2)?.submitted == true
loadData()
else if (seg.source as? ViewController2)?.deleted == true
shouldDisplayDeletedAlert = true
【讨论】:
@iosSwiftLearner isviewDidAppear
在unwindToMyHours
之后被调用?
是的,但是 xcode 再次跳过警报视图显示的代码
@iOSSwiftLearner 很难说没有更多信息。警报代码看起来不错,对我有用以上是关于警报框未显示。 X 代码是跳过 Swift 中的警报框代码的主要内容,如果未能解决你的问题,请参考以下文章