我知道如果我的项目中有navigationController,我可以使用popToRootViewController,但我的整个项目基于presentViewControllers,并且有一个场景是我导航到多个级别。如果只有一层,我可以通过关闭当前 View Controller 来返回 Root View Controller ,但当我下降到不止一层时,我无法弄清楚如何导航回 Root View Controller 。有人可以在这里给我建议吗?
我尝试使用以下代码,但我的应用程序崩溃了!
- (IBAction)mainMenuButtonPressed:(id)sender {
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[self presentModalViewController:delegate.window.rootViewController animated:YES];
}
谢谢。
最佳答案
如果我理解正确,那么您正在呈现几个模态视图 Controller 并想要返回到 Root View Controller 。如果这是正确的,那么以下代码应该适合您:
- (IBAction)mainMenuButtonPressed:(id)sender {
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[delegate.window.rootViewController dismissModalViewControllerAnimated:YES];
}
关于iOS:用于presentViewControllers架构的popToRootViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9913147/