ios 如何返回到Objective-C中的先前视图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11392567/
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
How to go back to previous view in Objective-C?
提问by user1490535
I am a beginner in iOS programming and I want to implement the functionality of going back to home view.
我是iOS编程初学者,想实现返回首页的功能。
I have use this code:
我已经使用了这个代码:
-(IBAction)onclickhome:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
I have used navigationController in home button click event but it is not jump to home screen.
我在主页按钮点击事件中使用了 navigationController 但它没有跳转到主屏幕。
Do you have any suggestion and source code which applies to my code?
您有任何适用于我的代码的建议和源代码吗?
回答by Lorenzo B
I'm not sure I understand your question.
我不确定我是否理解你的问题。
What do you mean with
你是什么意思
I have used navigationController in home button click event but it is not jump to home screen
我在主页按钮点击事件中使用了 navigationController 但它没有跳转到主屏幕
?
?
If you want to pop to the root controller you can use popToRootViewControllerAnimated
.
如果你想弹出到根控制器,你可以使用popToRootViewControllerAnimated
.
[self.navigationController popToRootViewControllerAnimated:YES];
From Apple doc of UINavigationController
来自苹果文档 UINavigationController
popToRootViewControllerAnimated:
Pops all the view controllers on the stack except the root view controller and updates the display.
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
If you have set up a UINavigationController
and its root controller is called A, then if you navigate from A to B and then from B to C you have two possibilities to come back to a previous controller (you can have others but I list the main ones):
如果您设置了 aUINavigationController
并且它的根控制器称为 A,那么如果您从 A 导航到 B,然后从 B 导航到 C,您有两种可能返回到以前的控制器(您可以有其他控制器,但我列出了主要控制器) ):
- navigate back from C to B with
popViewControllerAnimated
- navigate back from C to A with
popToRootViewControllerAnimated
- 从 C 导航回 B
popViewControllerAnimated
- 从 C 导航回 A
popToRootViewControllerAnimated
回答by user2734323
The best way to navigate back and forth views is by pushing and popping views from navigation view controller stack.
来回导航视图的最佳方法是从导航视图控制器堆栈中推送和弹出视图。
To go one view back, use below code. This will ensure that the smooth transition between views are retained.
走一回视,下面的代码使用。这将确保保留视图之间的平滑过渡。
UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:YES];
To go two views back, do as below
要返回两个视图,请执行以下操作
UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:NO];
[navigationController popViewControllerAnimated:YES];
If you aren't going back to your expected view, execute below code before popping any of the views...
如果您不返回预期视图,请在弹出任何视图之前执行以下代码...
UINavigationController *navigationController = self.navigationController;
NSLog(@"Views in hierarchy: %@", [navigationController viewControllers]);
You should see an array like the below one, which will help you in verifying the stack is maintained as expected.
您应该会看到如下所示的数组,这将帮助您验证堆栈是否按预期维护。
Views in hierarchy: (
"<Main_ViewController: 0x1769a3b0>",
"<First_ViewController: 0x176a5610>",
"<Second_ViewController: 0x176af180>"
回答by axetroll
I use
我用
[self dismissViewControllerAnimated:YES completion:nil];
since others answers didn't worked in Xcode 8.0
因为其他答案在 Xcode 8.0 中不起作用