从模态popup到根视图控制器
我正在尝试使用以下代码popup到根视图控制器:
self.navigationController!.popToRootViewController(animated: true)
这通常起作用,但是当当前视图是模态的时候,当试图使用这个代码的时候出现错误。 在这种情况下,我该如何回到根视图控制器?
提前致谢。
你可以检查当前的控制器是否呈现,如果呈现,则解除它,然后转到rootViewController
其他明智的去直接rootViewController
if self.presentingViewController != nil { self.dismiss(animated: false, completion: { self.navigationController!.popToRootViewController(animated: true) }) } else { self.navigationController!.popToRootViewController(animated: true) }
你已经提交了一个ViewController,所以你需要解雇它。
要快速closuresViewController,请使用以下命令:
self.dismiss(animated: true, completion: nil)
结果:
码
假设您的模态视图具有关联的下面的ViewController。
基本上首先隐藏你的视图,显示为一个模态 ,你可以从你的ViewController实例中使用dismiss(animated: Bool)
方法。
对于呈现为推送的视图,您可以从您的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) } } }
如何使用通知?
你的模式只是发布通知
然后你的NavigationController接收到,然后popup到rootViewController
你可以做这样的事情:
Objective-C的:
[(UINavigationController *)self.presentingViewController popToRootViewControllerAnimated:NO]; [self dismissViewControllerAnimated:YES completion:nil];
查看答案以供参考:
ios:如何解除模式视图控制器,然后popup推视图控制器