2

我知道如果我的项目中有 navigationController,我可以使用 popToRootViewController,但我的整个项目是基于 presentViewControllers 的,并且存在我导航到多个级别的场景。如果只有一个级别,我可以通过关闭当前视图控制器返回根视图控制器,但是当我下降到多个级别时,我无法弄清楚如何导航回根视图控制器。有人可以在这里给我建议吗?

我尝试使用以下代码,但我的应用程序崩溃了!

- (IBAction)mainMenuButtonPressed:(id)sender {
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [self presentModalViewController:delegate.window.rootViewController animated:YES];
}

谢谢。

4

2 回答 2

4

如果我理解正确,那么您将展示几个模态视图控制器并希望回到根视图控制器。如果这是正确的,那么以下代码应该适合您:

- (IBAction)mainMenuButtonPressed:(id)sender {
    MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
    [delegate.window.rootViewController dismissModalViewControllerAnimated:YES];
}
于 2012-03-28T18:14:13.077 回答
0

斯威夫特版本

var = APP_DELEGATE = UIApplication.sharedApplication().delegate as! AppDelegate
APP_DELEGATE.window?.rootViewController?.dismissViewControllerAnimated(true, completion: nil)

斯威夫特3

let APP_DELEGATE = UIApplication.shared.delegate as! AppDelegate
            APP_DELEGATE.window?.rootViewController?.dismiss(animated: true, completion: nil)
于 2015-10-30T10:06:43.720 回答