今天写页面调用presentViewController方法时遇到这样的错误。
'Application tried to present modally an active controller
.'
大概原因也是像描述的那样,就是我们调用显示当前的VC(viewController)。
这可怎么办?
先搞清楚我的大致环境吧。
1.程序中的UITabBarController是自己写的一个VC
self.window.rootViewController = _tabBarController;
self.tabBarController.tabBar.hidden
= YES;
... ...
[self.tabBarController.view
addSubview:_barController.view];
2.需要调用presentViewController方法的页面,也是由多个VC组合到该页面中去的。
鉴于这种情况,
1.直接用方法:
[self presentViewController:filmRootView
animated:YES
completion:^(void){
// Code
}];
不能够达到目的,因为,多个VC都集中在该页面中,所以,无法完成。
2.使用另一种方法
FilmRootView *filmRootView =
[[FilmRootView alloc] init];
[filmRootView refreshData:self.arrForPhoto];
UINavigationController
*navigationController = [[UINavigationController alloc] initWithRootViewController:filmRootView];
[filmRootView release];
[filmRootView setModalTransitionStyle:UIModalPresentationFullScreen];
[self presentViewController:navigationController
animated:YES
completion:^(void){
// Code
}];
[navigationController
release];
http://s5/mw690/7b9d64aftd3ce0de7a924&690tried
to present modally an active controller" TITLE="Application tried to present modally an active controller" />
显然不行。而且成了“夹心饼干”。仔细看代码,还是跟第一种类似,只不过,是重新声明了一个UINavigationController,并且更加糟糕。
其实,也还是第一种情况,多个VC并存下,导致,无法将新的VC,弹出到单独的页面。
不放弃!查啊!查!!!
大概这种情况很怪,确实没有查到什么!
不放弃!
继续找!
灵光一闪………………
于是,恍然大悟:
既然这个VC,由多个VC构成,那么,应该找到“根VC”,用“根VC”来调用presentViewController方法。
所以,在程序中,先找到根VC:
-
(id)getCurNavController{
UINavigationController* navController =
(UINavigationController*)self.tabBarController.selectedViewController;
return navController;
}
用返回的“根VC”,来调用presentViewController方法
FilmRootView *filmRootView=[[FilmRootView alloc]
init];
[filmRootView refreshData:self.arrForPhoto];
_appDelegate =
(AppDelegate*)[[UIApplication sharedApplication] delegate];
UINavigationController *theNavController=
[_appDelegate getCurNavController];
[theNavController
presentViewController:filmRootView
animated:YES
completion:^(void){
// Code
}];
成功!
我了个汗啊!起码绕了很久,必须mark一下!
希望对你有所帮助!
无意间,又翻出来看,发现,做一个修正。
这样做也是可以的。
根VC+ UINavigationController
FilmRootView *filmRootView=[[FilmRootView alloc]
init];
[filmRootView refreshData:self.arrForPhoto];
UINavigationController
*navigationController = [[UINavigationController alloc] initWithRootViewController:filmRootView];
[filmRootView setModalTransitionStyle:UIModalPresentationFullScreen];
[filmRootView release];
_appDelegate =
(AppDelegate*)[[UIApplication sharedApplication] delegate];
UINavigationController *theNavController=
[_appDelegate getCurNavController];
[theNavController
presentViewController:navigationController
animated:YES
completion:^(void){
// Code
}];
还是多看API好处多多啊!
加载中,请稍候......