Austin Berenyi
Austin Berenyi

Reputation: 1053

How do I present a UITabBarController at a specific index?

Right now I have my user finish onboarding from a UIPageViewController and tap a "FINISHED" button to go to the profile tab of my UITabBarController. However, the profile tab is at index [2], and the code below is taking me to index [0].

UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"tabHostController"];
[self presentViewController:vc animated:YES completion:nil];

How do I present the UITabBarController at a specific index [2] after tapping the "FINISHED" button from the UIPageViewController?

Answers in objective-c, please.

Upvotes: 0

Views: 342

Answers (2)

Shehata Gamal
Shehata Gamal

Reputation: 100533

You can change selectedIndex property of the TabBarController to make it open the index you want

UITabBarController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"tabHostController"];
vc.selectedIndex = 2; // or [vc setSelectedIndex:2];
[self presentViewController:vc animated:YES completion:nil];

Upvotes: 2

Nitish
Nitish

Reputation: 14123

I used the following in one of my app where I had to display 3rd tab. Nothing more than one line of code in viewDidLoad of TabBarController :

[self setSelectedIndex:2];  

And if you have to present 3rd tab only at the start i.e. after Onboarding and for rest of the launches you have to present 1st tab then in the viewDidLoad of 3rd tab's UIViewController, assign a boolean NSUserDefault :

[[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"loadThirdTab"];  

So now your viewDidLoad for TabBarController will be :

if( [[NSUserDefaults standardUserDefaults]objectForKey:@"loadThirdTab"]] != nil &&[[NSUserDefaults standardUserDefaults]boolForKey:@"loadThirdTab"])
   [self setSelectedIndex:0];
else
    [self setSelectedIndex:2];

Upvotes: 1

Related Questions