Reputation: 1246
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
Upvotes: 1
Views: 1569
Reputation: 1306
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;
}
Upvotes: 1
Reputation: 1751
I think Both if and else if are not satisfied with this condition
Just Check your Tag with this NSLog(@"%d",viewController.view.tag);
Upvotes: 0
Reputation: 28720
May be your array's are empty. Try to set a breakpoint and you will find the solution which line is causing the crash.
Upvotes: 0