【发布时间】:2020-10-01 09:50:52
【问题描述】:
我正在尝试关闭当前正在呈现UIAlertController 的UIViewController,如下所示,
class SampleViewController: UIViewController {
private var alertController: UIAlertController?
override func viewDidLoad() {
super.viewDidLoad()
presentAlert()
}
fucn presentAlert() {
let alertController = UIAlertController(title: "alert", message: nil, preferredStyle: .alert)
alertController.addAction(UIAlertAction(title: "Dismiss", style: .destructive, handler: {
dismiss(animated: true)
}))
alertController.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
present(alertController, animated: true, completion: {
self.alertController = alertController
})
}
override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
// alertController?.dismiss(animated: true)
/*This dismisses SampleViewController when user taps Cancel
but I want user to stay on this screen when they tap Cancel*/
super.dismiss(animated: flag, completion: completion)
}
}
class currentViewController: UIViewController {
private let sampleViewController = SampleViewController()
func presentSampleViewController() {
present(sampleViewController, animated: true)
}
func dismissSampleViewController() {
sampleViewController.dismiss(animated: true)
}
}
但只有警报被解除,而不是整个SampleViewController 我也无法找到是否从CurrentViewController 或UIAlertController 操作调用override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)。
我想同时关闭alert 和SampleViewController 并将用户带回CurrentViewController。任何帮助将不胜感激,在此先感谢。
【问题讨论】:
标签: ios swift xcode uiviewcontroller