呈现 UIAlertController 时无法关闭 UIViewController
Unable to dismiss UIViewController when it is presenting UIAlertController
我正在尝试关闭当前正在呈现 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
我也找不到 override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
是从 CurrentViewController
还是 UIAlertController
操作中调用的。
我想同时关闭 alert
和 SampleViewController
,并将用户带回 CurrentViewController
。任何帮助将不胜感激,提前致谢。
在 dismissSampleViewController()
中调用 dismiss(animated: flag, completion: completion)
而不是 sampleViewController.dismiss(animated: true)
关闭 CurrentViewController
您将关闭所有子视图控制器
我正在尝试关闭当前正在呈现 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
我也找不到 override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil)
是从 CurrentViewController
还是 UIAlertController
操作中调用的。
我想同时关闭 alert
和 SampleViewController
,并将用户带回 CurrentViewController
。任何帮助将不胜感激,提前致谢。
在 dismissSampleViewController()
中调用 dismiss(animated: flag, completion: completion)
而不是 sampleViewController.dismiss(animated: true)
关闭 CurrentViewController
您将关闭所有子视图控制器