Tab Bar Controller (storyboard template) and AppDelegate

11,190

Solution 1

Assuming things are set up in your storyboard as expected, this should give you a reference to the tab bar controller in didFinishLaunchingWithOptions::

NSLog(@"Root: %@", self.window.rootViewController);
UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;

Usually, you could get the current controller using...

self.currentController = [tabController selectedViewController];

...but since no controller has been selected at the time this method executes, the best guess of what you want is...

self.currentController = [[tabController viewControllers] objectAtIndex:0];

Solution 2

Use self.window.rootViewController.tabBarController to gain access to that view controller’s tab bar controller.

Share:
11,190
ingenspor
Author by

ingenspor

Updated on June 23, 2022

Comments

  • ingenspor
    ingenspor about 2 years

    When I create a XCode 4 iPhone template for TabBarController with storyboard, its automaticly configured with a main view controller and everything. But there is no propery for the Tab Bar Controller in the AppDelegate. Can I created an outlet for it, and tryed to link it with my Tab Bar Controller in storyboard but that's not possible. Is there a better way to access the Tab Bar Controller in didFinishLaunchingWithOptions method, as it's already kind of hooked up? What I want is self.currentController = current tab in Tab Bar Controller.

    AppDelegate.h:

    #import <UIKit/UIKit.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    @end
    

    AppDelegate.m:

    @interface AppDelegate()
    
    @property (nonatomic, assign) UIViewController<SubViewContainer> *currentController;
    
    @end
    
    @synthesize window = _window
    @synthesize currentController;
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    
    //I need this piece of code to equal the Tab Bar Controller current tab
    self.currentController = ?
    
    return YES;
    }
    
    //And I'm gonna use this void for some statements about the Tab Bar Controller tabs:
    - (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:
    (UIViewController *)viewController
    {
    // with some statements
    }
    
  • ingenspor
    ingenspor almost 12 years
    Ok thanks, so if I want self.currentController = current tab in Tab Bar Controller, any suggestion for how to do so?
  • Phillip Mills
    Phillip Mills almost 12 years
    Once you have the tab bar controller, you can get its selectedViewController property. Is that what you're asking?
  • ingenspor
    ingenspor almost 12 years
    I updated my question so it's more clearly. I'm new on this so I don't quite understand how to use you answer in my code but it looks right in the log as it says Root: UITabBarController.
  • Phillip Mills
    Phillip Mills almost 12 years
    But self isn't a view controller in this case.
  • The Kraken
    The Kraken almost 12 years
    I assume you're trying to access the tab bar controller from the App Delegate then. Try the updated code.
  • ingenspor
    ingenspor almost 12 years
    Yup, that's what I am trying to do. But pasting self.window.rootViewContoller.tabBarController in there isn't working. I took away my tabBarController property/synthesize/delegate code from appdelegate as I'm not supposed to create one. But still I can't reference to my remaining one in the appdelegate, though it says "Root: UITabBarController if i NSLog self.window.rootViewController.
  • ingenspor
    ingenspor almost 12 years
    Thanks! It getting close, but it says "Incompatible pointer types initializing UITabBarController with an expression of type UIViewController." Maybe I must somehow add the TabBarController as a subview of main window?
  • Phillip Mills
    Phillip Mills almost 12 years
    Cast the value you're assigning to get rid of the compiler warning. It's just complaining that you know the type of controller and it doesn't. :-)
  • The Kraken
    The Kraken almost 12 years
    Looks like @PhillipMills's answer worked out. It looks like a much more elegant solution. I apologize if I was confused with what exactly you were asking. Glad it worked out!