Swift 3如何在UIAlertController动作之后解除UIViewController

时间:2017-03-22 19:54:43

标签: swift3 uialertcontroller

我有一个警报控制器,想按OK后关闭视图控制器,但无法弄清楚如何。 self.dismiss(animated: true)仅关闭警报控制器本身。这是我的代码。

let alertController: UIAlertController = UIAlertController(title: "Password updatd", message: "Your password has been changed successfully", preferredStyle: .alert)

let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in }

alertController.addAction(okAction)

self.present(alertController, animated: true, completion: nil)

我试过这个,它只删除了当前的视图控制器,但没有回到根视图控制器。

let okAction: UIAlertAction = UIAlertAction(title: "OK", style: .default) { action -> Void in
self.navigationController?.popToRootViewController(animated: true)}

这给了我一个警告expression of type UIViewController is unused

1 个答案:

答案 0 :(得分:2)

导航控制器:

上一个控制器

self.navigationController?.popViewController(animated: true)

根控制器

self.navigationController?.popToRootViewController(animated: true)

模态视图控制器:

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