Jared Price
Jared Price

Reputation: 5375

Switching to a UITabBarController at a specific index

I'm trying to switch back to a UITabBarController at a specific index. My method does not appear to be working. Here is my code:

BaseViewController.m

-(void)switchTabView: (NSString*) viewName inx: (int) index
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    UITabBarController *view = [storyBoard instantiateViewControllerWithIdentifier:viewName];
    view.tabBarController.selectedIndex = index;
    [self presentViewController:view animated:YES completion:nil];
}

ChildViewController.m (inherits BaseViewController)

- (IBAction)backButtonClicked:(id)sender
{
    [self switchTabView:@"tabView" inx:1];
}

It goes back to the UITabBarController, but the index argument does not seem to matter since it always goes to index 0.

Upvotes: 0

Views: 110

Answers (1)

Avt
Avt

Reputation: 17043

Try to change

-(void)switchTabView: (NSString*) viewName inx: (int) index
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    UITabBarController *view = [storyBoard instantiateViewControllerWithIdentifier:viewName];
    view.tabBarController.selectedIndex = index;
    [self presentViewController:view animated:YES completion:nil];
}

to

-(void)switchTabView: (NSString*) viewName inx: (int) index
{
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
    UITabBarController *view = [storyBoard instantiateViewControllerWithIdentifier:viewName];
    view.selectedIndex = index;
    [self presentViewController:view animated:YES completion:nil];
}

Upvotes: 2

Related Questions