Accessing UITabBarController from UIVIewController

后端 未结 2 1303
清酒与你
清酒与你 2021-02-02 12:25

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

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

I need

2条回答
  •  遥遥无期
    2021-02-02 12:37

    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;
    

提交回复
热议问题