Accessing UITabBarController from UIVIewController

23,261

Solution 1

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;

Solution 2

You are doing it wrong.

I've an app same as yours. I can access tabbar from viewDidLoad.

Try this:

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tabBarController setSelectedIndex:1];
}

Hope this helps.. :)

Share:
23,261

Related videos on Youtube

rustylepord
Author by

rustylepord

Updated on July 14, 2020

Comments

  • rustylepord
    rustylepord almost 4 years

    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;
    }
    
    • nburk
      nburk over 9 years
      it's possible to just use self.tabBarController from any UIViewController that is embedded in the UITabBarController, check @Rashad's answer below
    • Milan Kamilya
      Milan Kamilya almost 5 years
      @Rashad's answer is correct , please check
  • rustylepord
    rustylepord about 10 years
    You are right moving in to viewDidLoad helped. Any particular reason why we can't access it during the initialization ?
  • Ace Green
    Ace Green almost 9 years
    This doesn't work on Xcode 7 beta 6. can anyone else confirm this works?
  • Dibzmania
    Dibzmania over 6 years
    This does not work on xCode 9. Something this basic is screwed up :(