代码之家  ›  专栏  ›  技术社区  ›  Pugalmuni Tom Tharakan

如何在iphone的Tabbar中更改选中的tab项?

  •  0
  • Pugalmuni Tom Tharakan  · 技术社区  · 14 年前

    我在视图控制器中以编程方式创建了选项卡栏。在我的应用程序中,最初显示选项卡栏,视图中有五个选项卡栏项。使用选项卡栏,最初选择第一个选项卡项。现在我想更改所选的项目,例如,第四个项目最初被选中
    我如何做到这一点?

    这是我的密码,

        tBar = [[UITabBarController alloc] init];
    
        first = [[first alloc] initWithNibName:@"first" bundle:nil];
    
        UINavigationController *navFirst = [[[UINavigationController alloc] initWithRootViewController:first] autorelease];
    
        second = [[second alloc] initWithNibName:@"second" bundle:nil];
    
        UINavigationController *navsecond = [[[UINavigationController alloc] initWithRootViewController:second] autorelease];
    
        third = [[third alloc] initWithNibName:@"third" bundle:nil];
    
       UINavigationController *navthird = [[[UINavigationController alloc] initWithRootViewController:third] autorelease];
    
       fourth = [[fourth alloc] initWithNibName:@"fourth" bundle:nil];
    
        UINavigationController *navfourth = [[[UINavigationController alloc] initWithRootViewController:fourth] autorelease];
    
       fifth = [[fifth alloc] initWithNibName:@"fifth" bundle:nil];
    
       UINavigationController *navfifth= [[[UINavigationController alloc] initWithRootViewController:fifth] autorelease];
    
       tBar.viewControllers = [NSArray arrayWithObjects:navFirst,navsecond, navthird, navfourth, navfifth,nil]; 
    
       [self.view addSubview:tBar.view];
    
       [self.navigationController pushViewController:tBar animated:NO];
    

    请参见下图。我要的就是这样,

    image http://www.freeimagehosting.net/uploads/2798661c55.png

    请引导我。

    谢谢。

    2 回复  |  直到 14 年前
        1
  •  0
  •   domino    14 年前

    试试这个:

    YourAppNameDelegate *appDelegate = (YourAppNameDelegate *)[[UIApplication sharedApplication] delegate];
    [appDelegate tabBarController].selectedIndex = 3;
    

    tBar.selectedIndex = 3;
    
        2
  •  0
  •   Oscar    13 年前

    如果要以编程方式从一个项目更改为另一个项目,也可以执行以下操作:

    [self parentViewController].yourTabBar.selectedIndex = 3;
    

    我希望这能帮助别人