关于解决MGSPlitview iOS版本兼容性问题

本文提供了解决在iOS5环境下,应用从iOS4.3升级后出现UIViewControllerHierarchyInconsistency错误的方法。通过代码修改确保NavigationController与rootViewController之间的正确层级关系,从而避免应用崩溃。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题:运行在4.3模拟器上的程序,到5.01失败了,错误信息如下


MGSplitView[839:f803] *** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<RootViewController: 0x6886380> should have parent view controller:<MGSplitViewController: 0x6ca1c00> but actual parent is:<UINavigationController: 0x6886980>'


解决方案:

UINavigationController *test=[[UINavigationController alloc] initWithRootViewController:detailViewController];
    test.navigationBar.hidden=YES;
    [test retain];
	//invokes a method of the receiver on the current thread using the default mode after a delay.
	[rootViewController performSelector:@selector(selectFirstRow) withObject:nil afterDelay:0];
	[detailViewController performSelector:@selector(configureView) withObject:nil afterDelay:0];
    
 self.splitViewController.viewControllers=[[NSArray alloc] initWithObjects:
rootViewController
,test, nil];



代码修改:

 UINavigationController *root=[[UINavigationController alloc] initWithRootViewController:rootViewController]
 UINavigationController *test=[[UINavigationController alloc] initWithRootViewController:detailViewController];
    test.navigationBar.hidden=YES;
    [test retain];
	//invokes a method of the receiver on the current thread using the default mode after a delay.
	[rootViewController performSelector:@selector(selectFirstRow) withObject:nil afterDelay:0];
	[detailViewController performSelector:@selector(configureView) withObject:nil afterDelay:0];
    
    self.splitViewController.viewControllers=[[NSArray alloc] initWithObjects:root,test, nil];


看完下图就明白为什么这么修改了:


NavigationController的rootviewController 对应了class:rootviewController //

在nib中的 结构,和用代码修改后的view controller结构不一样了,因此在ios 5的情况下,检查严格了,就出错了:


也有一些提出的解决此类问题的方法:

self.view=xxxxxcontroller.view

替换为:self.view addsubview:xxxcontroller.view


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值