TabBarController returns null

I have a tabbarcontroller in the storyboard as the initial view of the controller

enter image description here

enter image description here

How does this return zero

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 zeroing?

+6
source share
3 answers

The reason null returns is that self is your UITabBarController , so it has nothing 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]; 
+6
source

Check out Joe Answer self.tabBarController NULL

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

  @property (nonatomic, retain) UITabBarController * myTabBarController; 

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

 self.myTabBarController = self; self.myTabBarController.delegate = self; 
+1
source

In Swift

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

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

0
source

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


All Articles