Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing UITabBarController from UIVIewController

Tags:

I am developing an application based on UITabbar and the view hierarchy as follows.

UITabBarController ----> UINavigationController ----> UIViewController

I need to access the UITabBarController from the UIIVewController . But following properties always returns nil.

self.tabBarController and self.navigationController.tabBarController

Is there a way to access the Tabbarcontroller directly from a child viewController without using the AppDelegate ?

@implementation HomeViewController  - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];     if (self) {         // Custom initialization         self.title = @"Home";         self.navigationItem.title = @"Home";          self.tabBarItem.image = [UIImage imageNamed:@"TabBarHome"];          UITabBarController *tab = self.tabBarController;          UITabBarController *tab1 = self.navigationController.tabBarController;         UITabBarController *tab2 = self.navigationController.presentingViewController;        }     return self; } 
like image 687
rustylepord Avatar asked Apr 02 '14 09:04

rustylepord


Video Answer


1 Answers

With the hierachy that you are using:

enter image description here

I can acces without problem the UITabBarController from the ViewController with:

self.tabBarController

Move your Custom initialization to viewDidLoad or viewDidAppear

Then for shure you can access TabBarController with self.tabBarController

Another way to arrive to your TabBarController is:

UITabBarController *tabBarController = (UITabBarController *)[[[UIApplication sharedApplication] delegate] window].rootViewController; 

But it is totally unnecessary in your case.

EDIT:

If you are working with Xib, then you has been created a TabBarController programmatically in your AppDelegate. I'm sure you have something like:

self.tabBarController = [[UITabBarController alloc] init];

Then you can call it in your ViewController:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; UITabBarController *tabBarController = appDelegate.tabBarController; 
like image 199
Gabriel.Massana Avatar answered Oct 14 '22 06:10

Gabriel.Massana