首页 > 解决方案 > 停止 UI 警报控制器返回上一个视图

问题描述

我的应用程序中有一个 UIAlertView。但是出于某种奇怪的原因,如果我决定按下警报视图上的“返回”按钮。它实际上把我带到了前一个视图控制器,而不是简单地解除警报。

我怎样才能防止这种情况?如果按下后退按钮,我只想隐藏警报

问题视频:https ://youtu.be/fhb-REuQiyg

这是警报的功能:

let alertController = UIAlertController.init(title: "Open Map", message: "", preferredStyle: .alert)
alertController.addAction(UIAlertAction.init(title: "Google Maps", style: .default, handler: { (action) in
    self.googleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Apple Maps", style: .default, handler: { (action) in
    self.appleMapsOpen()
}))
alertController.addAction(UIAlertAction.init(title: "Back", style: .default, handler: { (action) in
    self.dismiss(animated: true, completion: nil)

}))
self.present(alertController, animated: true) {
}

标签: iosswiftuialertcontroller

解决方案


删除这个

self.dismiss(animated: true, completion: nil)

这里

alertController.addAction(UIAlertAction.init(title: "Back", style: .default, handler: { (action) in
    self.dismiss(animated: true, completion: nil) 
}))

当您按下操作时,警报控制器会自动关闭,因此您的书面关闭也会关闭当前呈现的 vc


推荐阅读