在项目开发过程中,我遇到这样的一个问题:架构是tabViewController+NavigationViewController,在一个子控制器中我需要一个alertview点击后响应[self.navigationControllerpopToViewController:[self.navigationController.viewControllersobjectAtIndex:i]animated:YES];
结果报错:reason: '-[_UIAlertShimPresentingViewController setHide:]: unrecognized selector sent to instance 0x146dfeb0'
从报错的信息来看是_UIAlertShimPresentingViewController没有定义setHide函数,而我的setHide函数是定义在我的tabViewController里的,
后来追溯原因发现是因为我的子控制用到了以下两个函数(用来控制自定义的tabbar是否显示)
-(void)viewWillAppear:(BOOL)animated
{
tabViewController*tabbarcontroller = (tabViewController*)[UIApplicationsharedApplication].keyWindow.rootViewController;
[tabbarcontrollersetMyHide:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
tabViewController*tabbarcontroller = (tabViewController*)[UIApplicationsharedApplication].keyWindow.rootViewController;
[tabbarcontrollersetMyHide:NO];
}
后来发现先弹出了个alertview,再去使用keywindow,此时的keywindow.rootviewcontroller就是UIAlertShimPresentingViewController,因此无法执行setHide函数。
我的解决方案时不使用[UIApplicationsharedApplication].keyWindow.rootViewController去获取根视图,通过
self.view.window.rootViewController去获取根视图。
代码如下:
-(void)viewWillAppear:(BOOL)animated
{
tabViewController*tabbarcontroller = (tabViewController*)[UIApplicationsharedApplication].keyWindow.rootViewController;
[tabbarcontrollersetMyHide:YES];
}
-(void)viewWillDisappear:(BOOL)animated
{
tabViewController*tabbarcontroller = (tabViewController*)self.view.window.rootViewController;
[tabbarcontrollersetMyHide:NO];
}