我试图使用以下代码弹出到根视图控制器:
self.navigationController!.popToRootViewController(animated: true)
Run Code Online (Sandbox Code Playgroud)
这通常有效,但在当前视图是模态时尝试使用此代码时出错.在这种情况下,如何弹回到根视图控制器?
提前致谢.
Nir*_*v D 23
您可以检查当前控制器是否出现,如果出现,则将其解除,然后rootViewController直接转到另一个控制器rootViewController
if self.presentingViewController != nil {
self.dismiss(animated: false, completion: {
self.navigationController!.popToRootViewController(animated: true)
})
}
else {
self.navigationController!.popToRootViewController(animated: true)
}
Run Code Online (Sandbox Code Playgroud)
Wil*_*son 10

假设你的模态视图与下面的ViewController相关联.
基本上首先要隐藏显示为模态的 View ,您可以使用dismiss(animated: Bool)ViewController实例中的方法.
并提出将在视图的推动下,你可以从你的navigationController属性使用这些方法如:popToRootViewController(animated: Bool),popViewController(animated:Bool)
class ModalViewController: UIViewController {
@IBAction func backButtonTouched(_ sender: AnyObject) {
let navigationController = self.presentingViewController as? UINavigationController
self.dismiss(animated: true) {
let _ = navigationController?.popToRootViewController(animated: true)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16173 次 |
| 最近记录: |