2010-08-04 94 views
0

我已经在视图控制器中以编程方式创建了标签栏。在我的应用程序中,初始显示标签栏,并在视图中有五个标签栏项目。使用标签栏,最初选择第一个标签项目。现在我想改变选择的项目,第四项最初被选中。(见图)
我该如何做到这一点?如何更改iphone中Tabbar中的选定选项卡项?

这里我的代码,

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

请指引我。

谢谢。

回答

0

试试这个:

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

tBar.selectedIndex = 3; 
+0

感谢您的快速回复。 – Pugal 2010-08-04 14:41:47

0

如果你想从另一个一个项目更改程序,你也可以这样做:

[self parentViewController].yourTabBar.selectedIndex = 3; 

我希望这有助于有人

相关问题