需要从呈现的UINavigationController中返回呈现UIViewController

时间:2014-05-14 15:24:02

标签: ios uiviewcontroller uinavigationcontroller xib uistoryboard

在MyMainViewController中,我提供了一个这样的导航控制器:

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

稍后,从UINavigationController的视图层次结构中的某个位置,我需要返回MyMainViewController。我怎么能这样做?

(注意:MyMainViewController是在.XIB中定义的,而不是在定义了UINavigationController及其子项的故事板中。)

2 个答案:

答案 0 :(得分:1)

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

我猜你知道你的视图控制器的索引。如果您只想返回rootViewController,可以像

一样
[self.navigationController popToRootViewControllerAnimated:YES];

如果你想将新的viewController推送到导航堆栈,就像这样做

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

返回上一个viewController将是

[self dismissViewControllerAnimated:YES completion:nil];

答案 1 :(得分:1)

听起来你已经模拟地提出了你想要删除的NavController。模态呈现的VC可以自行删除。

NavController中的某处添加:

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