Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
like image 921
Robert J. Clegg Avatar asked Mar 29 '14 17:03

Robert J. Clegg


1 Answers

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;
like image 106
Greg Avatar answered Sep 29 '22 23:09

Greg