切换到特定索引处的UITabBarController

时间:2014-02-17 22:13:37

标签: objective-c uitabbarcontroller

我正在尝试切换回特定索引处的UITabBarController。我的方法似乎不起作用。这是我的代码:

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(继承BaseViewController)

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

它返回到UITabBarController,但索引参数似乎并不重要,因为它总是转到索引0。

1 个答案:

答案 0 :(得分:2)

尝试更改

-(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];
}

-(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];
}