Mord Fustang Mord Fustang - 2 years ago 136
iOS Question

TabBarController returns null

I have a tabbarcontroller in storyboard as initial view controller

enter image description here

enter image description here

How does this return null

UITabBarController* tabbarController = (UITabBarController*) self.tabBarController;

Originally what I am trying to do

NSMutableArray *newTabs = [NSMutableArray arrayWithArray:[self.tabBarController viewControllers]];
NSLog(@"\n\n\n%lu\n\n\n",(unsigned long)[newTabs count]);
NSLog(@"self.tabBarController.viewControllers %@ \n\n",self.tabBarController);
[newTabs removeObjectAtIndex: 1];
[self.tabBarController setViewControllers:newTabs];

Why am I getting null?

Answer Source

The reason null is returned, is that self is your UITabBarController, therefore, it doesn't have anything on self.tabBarController

Your code should look like this:

NSMutableArray *newTabs = [NSMutableArray arrayWithArray:[self viewControllers]];
NSLog(@"\n\n\n%lu\n\n\n",(unsigned long)[newTabs count]);
NSLog(@"self.viewControllers %@ \n\n",self);
[newTabs removeObjectAtIndex: 1];
[self setViewControllers:newTabs];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download