Robert J. Clegg
Robert J. Clegg

Reputation: 7360

Accessing UITabBarController from Appdelegate

I am trying to access my UiTabBarController from a UIViewController that is in UIWindow,

Since its not part on UITabBarController - using self.tabBarController.. won't work as it will be nil.

So I tried this code:

BBAppDelegate *appDelegate = (BBAppDelegate *)[[UIApplication sharedApplication] delegate];

    UITabBarController *tabBarController = appDelegate.window.rootViewController.tabBarController;

When it step through the code with the debugger - I can see appDelegate does have a tabBarController and it is not nil. However on the next line

  UITabBarController *tabBarController = appDelegate.window.rootViewController.tabBarController;

This results in tabBarController instance being nil and the tabBarController in the appDelegate still have a memory address assigned to it and its not nil.

What am I doing wrong here?

Edit

Here is what my debugger looks like:

enter image description here Thanks!

Edit 2

This is who I setup the side menu which is added to rootViewController

UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPhone"
                                                         bundle: nil];

BBFilterViewController *loginController=[[UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"FilterViewController"];  UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:loginController];



self.tabBarController = [mainStoryboard instantiateViewControllerWithIdentifier: @"HomeScreen"];

MVYSideMenuOptions *options = [[MVYSideMenuOptions alloc] init];
options.contentViewScale = 1.0;
options.contentViewOpacity = 0.5;
options.shadowOpacity = 0.0;

MVYSideMenuController *sideBarController = [[MVYSideMenuController alloc]initWithMenuViewController:navController contentViewController:self.tabBarController options:options];


self.window.rootViewController = sideBarController;

[self.window.rootViewController addChildViewController:self.tabBarController];

[self.window makeKeyAndVisible];

Upvotes: 8

Views: 13601

Answers (2)

Greg
Greg

Reputation: 25459

If you have tab bar controller as root view controller you can access it like:

UITabBarController *tabBarController = (UITabBarController *)appDelegate.window.rootViewController;

//Extended

Based on update to your question you should be able to get reference to tab bar controller from last child root view controller array:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController.childViewControllers.lastObject;

Upvotes: 15

sahara108
sahara108

Reputation: 2859

From you debugger I think it should be UITabBarController *tabBarController = appDelegate.tabBarController;

Upvotes: -2

Related Questions