How to changed the selected tab items in the Tabbar in iphone?

a 夏天 提交于 2020-01-07 04:38:28

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!