App Crashes in UITabBarController delegate method

可紊 提交于 2019-12-11 04:35:58

问题


Hi I am trying to add and remove tab bar elements dynamically. There are two arrays. One is shown first with an added tabbaritem with name "More" and other array is added to the tabbar when user presses More. User can come back to first array by pressing Less tabbaritem in second array. Problem is that when i frequently press More and Less tabbaritems in sequence More, Less, More, Less, More, Less - The app crashes after last Less. Array seems ok to me and so is tabbar controller. I am not able to figure out the problem. Below is the code of tab bar delegate method.

 - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
 NSLog(@"selected view controller is :%@",viewController);
 if(viewController.view.tag == -1){
     [self.tabBarController setViewControllers:self.level2TabBarItems animated:YES];
     [self.tabBarController setSelectedIndex:0];
 }else if(viewController.view.tag == -2){
     [self.tabBarController setViewControllers:self.level1TabBarItems animated:YES];
     [self.tabBarController setSelectedIndex:0];
 }

}

Can anyone please let me know where I am doing wrong? Best Regards


回答1:


I had similar problem. I guess that you construct new instance of VC in your array, so frequently switching more/less causes calling method from the old instance (is not replaced yet at that moment).

Unfortunatelly setViewControllers method (as documentation say) automatically remove old view controllers calling dealloc and it seems that there is no other way to reuse them.

In your case you can try to disable selecting tabs until tabBarController:didSelectViewController: execute implementing (I didn't test it):

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    self.selectLock = YES;
    // your code
    self.selectLock = NO;
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    return !self.selectLock;
}



回答2:


Make comment NSLog here. This is not proper format to print this.




回答3:


May be your array's are empty. Try to set a breakpoint and you will find the solution which line is causing the crash.




回答4:


I think Both if and else if are not satisfied with this condition

Just Check your Tag with this NSLog(@"%d",viewController.view.tag);



来源:https://stackoverflow.com/questions/6186370/app-crashes-in-uitabbarcontroller-delegate-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!