Accessing UITabBarController from UIVIewController

对着背影说爱祢 提交于 2019-12-03 00:15:18

With the hierachy that you are using:

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;

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.. :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!