问题
I have created tab bar programmatically in the view controller. In my application, Initially displayed the tabbar and it has five tab bar items in the view.Using tab bar, Initially first tab item is selected. Now i want to change the selected items like, fourth item is initially selected.(See the image)
How do i achieve this?
Here my code is,
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];
See the below image. I want exactly like this,
image http://www.freeimagehosting.net/uploads/2798661c55.png
Please guide me.
Thanks.
回答1:
Try this:
YourAppNameDelegate *appDelegate = (YourAppNameDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate tabBarController].selectedIndex = 3;
or
tBar.selectedIndex = 3;
回答2:
If you want to change from a item to another programmatically you can also do this:
[self parentViewController].yourTabBar.selectedIndex = 3;
I hope this helps someone
来源:https://stackoverflow.com/questions/3406545/how-to-changed-the-selected-tab-items-in-the-tabbar-in-iphone