[英]How to go back to (currentView-2) UIViewController in Objective-C?
[英]How to go back to previous view in Objective-C?
我是iOS编程的初学者,我想实现回到主视图的功能。
我使用这段代码:
-(IBAction)onclickhome:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
我在主页按钮单击事件中使用了navigationController,但它没有跳转到主屏幕。
您有任何适用于我的代码的建议和源代码吗?
我不确定我理解你的问题。
你是什么意思
我在主页按钮单击事件中使用了navigationController,但它没有跳转到主屏幕
?
如果要弹出到根控制器,可以使用popToRootViewControllerAnimated
。
[self.navigationController popToRootViewControllerAnimated:YES];
来自Apple的UINavigationController
文档
popToRootViewControllerAnimated:
Pops all the view controllers on the stack except the root view controller and updates the display.
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated
如果您已经设置了UINavigationController
并且其根控制器被称为A,那么如果您从A导航到B然后从B导航到C,那么您有两种可能性来回到之前的控制器(您可以使用其他控制器,但我列出了主控制器)那些):
popViewControllerAnimated
从C导航回popViewControllerAnimated
popToRootViewControllerAnimated
从C导航回A 导航来回视图的最佳方法是从导航视图控制器堆栈中推送和弹出视图。
要返回一个视图,请使用下面的代码。 这将确保保留视图之间的平滑过渡。
UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:YES];
要返回两个视图,请执行以下操作
UINavigationController *navigationController = self.navigationController;
[navigationController popViewControllerAnimated:NO];
[navigationController popViewControllerAnimated:YES];
如果您没有回到预期的视图,请在弹出任何视图之前执行以下代码...
UINavigationController *navigationController = self.navigationController;
NSLog(@"Views in hierarchy: %@", [navigationController viewControllers]);
你应该看到一个类似下面的数组,它将帮助你验证堆栈是否按预期维护。
Views in hierarchy: (
"<Main_ViewController: 0x1769a3b0>",
"<First_ViewController: 0x176a5610>",
"<Second_ViewController: 0x176af180>"
我用
[self dismissViewControllerAnimated:YES completion:nil];
因为其他答案在Xcode 8.0中没有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.