Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically pressing a UITabBar button in Xcode

Sorry for the newbie question. I have a UITabBar in my main window view as well as an array of UINavigationControllers for each Tab. The structure is similar to the iPod app in that the main views can be seen by selecting TabBar items and then the user can drill down further with the NavigationController by pushing views to the stack.

What I would like to be able to do is to do the equivalent of pressing a TabBar button at the bottom from any of the subviews in code (i.e., change the selected property of the TabBar and display launch the first view controller for the tab).

Any help would be greatly appreciated.

Dave

like image 436
Magic Bullet Dave Avatar asked Nov 25 '09 08:11

Magic Bullet Dave


2 Answers

[myTabBarController setSelectedIndex:index]

EDIT: Answering the part 2 question from the comment:

You can define a method in AppDelegate for switching to a different tab.

And you can get hold of appdelegate from anywhere and send a message.. something like:

 MyAppDelegate *appDelegate = (MyAppDelegate*) [[UIApplication sharedApplication] delegate];
 [appDelegate SwitchToTab:index]
like image 75
prakash Avatar answered Sep 19 '22 12:09

prakash


alternatively...

[self.parentViewController.tabBarController setSelectedIndex:3];
like image 44
codemonkey Avatar answered Sep 20 '22 12:09

codemonkey