ios 在 Swift 上弹出 ViewController
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25400476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Popping ViewController on Swift
提问by erdemgc
I need to pop a UIViewController from the navigation controller.
我需要从导航控制器弹出一个 UIViewController。
Just writing this line of code but taking an exception;
只是写了这行代码,但有一个例外;
unexpectedly found nil while unwrapping an Optional value
在解开 Optional 值时意外发现 nil
self.navigationController.popViewControllerAnimated(true)
If I make the navigation controller optional, this line makes no effect, no popping
如果我将导航控制器设为可选,则此行无效,不会弹出
self.navigationController?.popViewControllerAnimated(true)
How to solve it?
如何解决?
回答by karlofk
You need to unwrap your navigationController correctly
您需要正确解开您的 navigationController
if let navController = self.navigationController {
navController.popViewController(animated: true)
}
回答by Anit Kumar
回答by spaceMonkey
In my case im using a Master Details view ( Split View Controller ). My details view controller is embedded inside an navigation controller. So when i wanted to dismiss my Details view controller. I had to pop it from the navigation controller of the parent (Split view controller) Like this.
在我的情况下,我使用主详细信息视图(拆分视图控制器)。我的详细信息视图控制器嵌入在导航控制器中。所以当我想关闭我的详细信息视图控制器时。我不得不像这样从父级(拆分视图控制器)的导航控制器中弹出它。
_ = self.navigationController?.navigationController?.popViewController(animated: true)
hope this helps someone.
希望这有助于某人。
回答by Aleksi Sj?berg
It seems that the view controller you're working with isn't embedded in Navigation Controller. If there was a Navigation Controller, i.e. self.navigationController
is not nil, both lines should work just as well even though the latter one is preferred as it uses optional chaining.
您正在使用的视图控制器似乎没有嵌入到导航控制器中。如果有一个导航控制器,即self.navigationController
不是 nil,那么两行都应该工作得一样好,即使后者是首选,因为它使用了可选链接。
Make sure you have embedded your View Controller in a Navigation Controller. You can do it by selecting the View Controller in Storyboard editor and clicking Editor -> Embed In -> Navigation Controller. Also make sure that you have your Storyboard Entry Point(the arrow that indicates which view controller is presented first) either pointing to Navigation Controller or before it.
确保您已将视图控制器嵌入到导航控制器中。您可以通过在 Storyboard 编辑器中选择 View Controller 并单击Editor -> Embed In -> Navigation Controller 来实现。还要确保您的Storyboard Entry Point(指示首先显示哪个视图控制器的箭头)指向 Navigation Controller 或在它之前。