In MyMainViewController, I present a navigation controller like this:

UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UINavigationController* nc = [storyboard instantiateViewControllerWithIdentifier:@"NAVIGATION_CONTROLLER_ID"];
[self presentViewController:nc animated:YES completion:nil];

Later, from somewhere within the view hierarchy of the UINavigationController, I need to return to MyMainViewController. How can I do this?

(Note: MyMainViewController is defined in a .XIB, and not in the storyboard where the UINavigationController and it's children are defined.)

有帮助吗?

解决方案

It sounds like you have modally presented a NavController that you want to remove. Modally presented VC's can remove themselves.

Somewhere in your NavController add:

[self dismissViewControllerAnimated:YES completion:^{
    NSLog(@"Dismissed nav controller modally");
}]

其他提示

[self.navigationController popToViewController:[[self.navigationController viewControllers] objectAtIndex:2] animated:YES];

I gues you know the index of your view controller. If you simply want to return to the rootViewController you can do it like

[self.navigationController popToRootViewControllerAnimated:YES];

If you want to push new viewController to the navigation stack just do it like

MyMainViewController *mainController = [[MyMainViewController alloc] initWithNibName:@"MyMainViewController" bundle:nil];
[self.navigationController pushViewController:desController animated:YES];

Returning to the previous viewController would be

[self dismissViewControllerAnimated:YES completion:nil];
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top