Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
NSLog(@"%@",tabbarController);
NSLog(@"%@",self.navigationController.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?

like image 316
Mord Fustang Avatar asked Oct 12 '15 19:10

Mord Fustang


3 Answers

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];
like image 107
Leonardo Avatar answered Nov 20 '22 04:11

Leonardo


In Swift

        let indexToRemove = 1
        if indexToRemove < (self.viewControllers?.count)! {
            var viewControllers = self.viewControllers
            viewControllers?.remove(at: indexToRemove)
            self.viewControllers = viewControllers
        }

directly use self in UITabBarController class, not self.tabBarControllers.

like image 24
Aditya Avatar answered Nov 20 '22 05:11

Aditya


Check out Joe's Answer self.tabBarController is NULL

If you have a navigation controller you need to add this to your YourTabBarViewController.h file

 @property (nonatomic, retain) UITabBarController * myTabBarController;

Then in YourTabBarViewController.m file in viewDidLoad just assign it to self and add the delegate

self.myTabBarController = self;
self.myTabBarController.delegate = self;
like image 1
Bryan Norden Avatar answered Nov 20 '22 03:11

Bryan Norden