我有带子的ViewController,它很好,但是当我将根视图更改为另一个时,然后返回根视图:
-(void)showSearchResultView
{
self.searchView.frame = self.view.frame;
__rootView = self.view;
[self setView:self.searchView];
}
-(void)hideSearchResultView
{
if(__rootView) [self setView:__rootView];
__rootView = nil;
}我得到了例外:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency',
reason: 'child view controller:<SlideViewController> should have parent view controller:
<UINavigationController> but actual parent is:<MainViewController>'使用子视图控制器更改根视图的正确方法是什么?
发布于 2013-07-27 15:00:43
我找到了解决办法。只需删除子根视图并再次添加它。
-(void)showSearchResultView
{
self.searchView.frame = self.view.frame;
__rootView = self.view;
[self setView:self.searchView];
for(UIViewController* vc in self.childViewControllers){
[vc.view removeFromSuperview];
}
}
-(void)hideSearchResultView
{
if(__rootView) [self setView:__rootView];
__rootView = nil;
for(UIViewController* vc in self.childViewControllers){
vc.view.frame = self.view.bounds;
[self.view addSubview:vc.view];
}
} 发布于 2013-07-27 14:04:31
要更改根视图,必须从导航堆栈中删除rootviewcontroller。比如:
NSMutablerray *控制器= self.navigationController视图控制器;
控制器removeObjectAtIndex:0;
它将删除rootviewController和名称方式,您可以添加新的视图控制器。
控制器addObjectAtIndex:0;
注意:-这个代码语法可能不正确,.this只是一种方法。
最新情况:-
NSMutableArray *viewControllers = [[NSMutableArray alloc]initWithArray:self.navigationController.viewControllers];
RootViewController *root = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
[viewControllers insertObject:root atIndex:0];
self.navigationController.viewControllers = viewControllers;这样您就可以更改根视图。
https://stackoverflow.com/questions/17897785
复制相似问题