我在视图控制器中以编程方式创建了标签栏。在我的应用程序中,最初显示标签栏,它在视图中有五个标签栏项目。使用标签栏,选择最初的第一个标签项。现在我想更改所选项目,例如,最初选择第四项。(见图)
我如何实现这一目标?
我的代码在这里,
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 :(得分:0)
试试这个:
YourAppNameDelegate *appDelegate = (YourAppNameDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate tabBarController].selectedIndex = 3;
或
tBar.selectedIndex = 3;
答案 1 :(得分:0)
如果您想以编程方式从项目更改为另一项,您也可以这样做:
[self parentViewController].yourTabBar.selectedIndex = 3;
我希望这有助于某人