ios 从导航控制器中关闭推送视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9444743/
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
Dismiss pushed view from within Navigation Controller
提问by Darren
I have a Navigation Controller with a View Controller displaying a button. The button is linked to another View Controller using a push segue which automatically adds a top navigation bar with a back button. This all works fine. Pressing the back button slides off the 2nd view and returns to the 1st.
我有一个带有显示按钮的视图控制器的导航控制器。该按钮使用 push segue 链接到另一个视图控制器,该按钮会自动添加带有后退按钮的顶部导航栏。这一切正常。按后退按钮滑出第二个视图并返回到第一个视图。
I have a button on the 2nd View Controller, that when pressed runs some code and a delegate call back to the 1st View Controller. Again this works fine.
我在第二个视图控制器上有一个按钮,按下时会运行一些代码和一个委托回调到第一个视图控制器。这再次工作正常。
Now I just need to dismiss the 2nd pushed View from code as if the back button was pressed. I have tried using dismissModalViewCcontrollerAnimated and dismissViewControllerAnimated, however they both dismiss the whole Navigation Controller which removes view 2 and 1 (returning bak to my main menu).
现在我只需要从代码中关闭第二个推视图,就像按下后退按钮一样。我试过使用dismissModalViewCcontrollerAnimated 和dismissViewControllerAnimated,但是它们都关闭了整个导航控制器,它删除了视图2 和1(返回到我的主菜单)。
Whats the correct way to slide off the view.
从视图中滑出的正确方法是什么。
回答by Stavash
Obtain a reference to your UINavigationController and call
获取对 UINavigationController 的引用并调用
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
on it.
在上面。
回答by Garrett Cox
In Swift it would be calling the method
在 Swift 中,它将调用该方法
self.navigationController?.popViewControllerAnimated(true)
回答by A.G
If we use push segue, then use popViewController
如果我们使用push segue,那么使用popViewController
@IBAction func backButtonClicked(_ sender: Any) {
self.navigationController?.popViewController(animated: false)
}
回答by jnwagstaff
In swift you can also call:
在 swift 你也可以调用:
self.navigationController?.popToRootViewControllerAnimated(true)
回答by Jan Damek
On Objective-C is
在 Objective-C 上是
[self.navigationController popViewControllerAnimated:YES];
for a jump to the first rootcontroller
跳转到第一个root控制器
[self.navigationController popToRootViewControllerAnimated:YES];
or is a possible move to the specific controller
或者可能转移到特定的控制器
[self.navigationController popToViewController:(nonnull UIViewController *) animated:(BOOL)];
animation specific animation process of move the controller. If the animation is falsethe controller will appear without animations.
The UIViewControllermust be from one which is on the stack.
动画 移动控制器的特定动画过程。如果动画是false控制器将显示没有动画。的UIViewController必须是从一个其是在堆栈中。
回答by arun-r
If NavViewController is used with UIModalPresentationFullScreen then the below line will work
如果 NavViewController 与 UIModalPresentationFullScreen 一起使用,则以下行将起作用
self.navigationController?.dismiss(animated: true, completion: nil)

