以编程方式调用 iOS 上的导航控制器后退按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2107876/
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
Programmatically call navigation controller back button on iOS
提问by oberbaum
In a UINavigationController-based iPhone app, in a method I would like to perform the programmatic equivalent of the back button being pressed and going back a view.
在基于 UINavigationController 的 iPhone 应用程序中,在一种方法中,我想执行与按下后退按钮并返回视图的编程等效的方法。
i.e. automatically press the Jobs button as seen here:
即自动按下作业按钮,如下所示:
Is there a generic iOS call I can make, or is more information required?
我可以进行通用的 iOS 调用,还是需要更多信息?
回答by Steve Harrison
UINavigationController
's -popViewControllerAnimated:
method should do what you want:
UINavigationController
的-popViewControllerAnimated:
方法应该做你想做的:
[navigationController popViewControllerAnimated:YES];
回答by Kevin Elliott
Assuming you don't actually want to PRESS the button programmatically, but simply copy the outcome of pressing the button, you should tell the navigation controller to pop the current view controller.
假设您实际上不想以编程方式按下按钮,而只是复制按下按钮的结果,您应该告诉导航控制器弹出当前视图控制器。
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController popViewControllerAnimated:YES];
This will remove it from the stack, and return you to the previous view controller.
这将从堆栈中删除它,并返回到上一个视图控制器。
回答by Tal Zion
Swift 3.0
斯威夫特 3.0
Back to the root view
回到根视图
self.navigationController?.popToRootViewController(animated: true)
Back to the previous view
返回上一个视图
self.navigationController?.popViewController(animated: true)
Swift 2.3
斯威夫特 2.3
Back to the root view
回到根视图
self.navigationController?.popToRootViewControllerAnimated(true)
Back to the previous view
返回上一个视图
self.navigationController?.popViewControllerAnimated(true)
回答by Niels Castle
You should call
你应该打电话
popViewControllerAnimated:
popViewControllerAnimated:
which is the opposite of adding view controllers with pushViewController:animated:
这与添加视图控制器相反 pushViewController:animated:
回答by Nishant Mahajan
[self.navigationController popViewControllerAnimates:YES];
is the best option but if you are nor on the same view controller class or your delegate changes before your back button method called then you can also try--
是最好的选择,但如果您不在同一个视图控制器类上,或者在调用后退按钮方法之前您的委托发生了变化,那么您也可以尝试--
first you have to define back button---
首先你必须定义后退按钮---
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"anyTitleForBackButton" style: UIBarButtonItemStyleBordered target: nil action: @selector(backButtonTapped)];
[[self navigationItem] setBackBarButtonItem: newBackButton];
[newBackButton release];
and then in backButtonTapped method you can call--
然后在 backButtonTapped 方法中你可以调用——
[self.navigationController pushViewController:desiredViewController animated:YES];