Application error in UITabBarController delegate method

Hi I'm trying to add and remove tab bar items dynamically. There are two arrays. First, the first added tabbaritem named Advanced is shown, and another array is added to the tab when the user clicks Advanced. The user can return to the first array by clicking Less tabbaritem in the second array. The problem is that when I often click the “More” and “less” tabbaritems in the sequences “More”, “Less”, “More”, “Less”, “Less”, “Less”. It seems to me that it seems to me that this is a cool class, and therefore this is a tab controller. I can not understand the problem. Below is the code for the delegation method of the tab bar.

- (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 someone please tell me where I am doing wrong? Best wishes

+1
source share
4 answers

I had a similar problem. I assume that you are creating a new instance of VC in your array, so frequent switching of larger / smaller calls the method from the old instance (at this point it is not yet replaced).

The Unfortunatelly setViewControllers method (as documentation ) automatically removes the old view controllers calling dealloc , and there seems to be no other way to reuse them.

In your case, you can try disabling the tab selection while tabBarController:didSelectViewController: execute the implementation (I have not tested 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; } 
+1
source

Make a comment on NSLog here. This is not a valid print format. A.

0
source

Maybe your array is empty. Try setting a breakpoint and you will find the solution that causes the crash.

0
source

I think both of them, if they are not satisfied with this condition

Just check your tag on NSLog(@"%d",viewController.view.tag);

0
source

Source: https://habr.com/ru/post/1302776/


All Articles