31

is it UITabBarController possible to select the tab in code?like UINavigationController we can use popViewController pop back to rootViewController instead of press the back button, so how can this implement in tabbarcontroller?

1 Answers1

76

Use the selectedIndex property on the UITabBarController. More information can be found in the UITabBarController Class Reference.

controller.selectedIndex = 4;

or use

[controller setSelectedIndex:4];

Gerco Dries
  • 6,682
  • 1
  • 26
  • 35
  • 2
    that should be [controller setSelectedIndex:4] – Jason Harwig May 21 '09 at 02:58
  • 7
    But that code doesn't trigger tabBarController:didSelectViewController: – gcstr Jun 11 '10 at 00:03
  • If you want to use the delegate method to do work, you could just call it yourself using `self.tabController(tabController, didSelect: controller)`. A bit hacky but it works. You should file a radar for it; setting the index _should_ call it automatically. – barndog Feb 28 '17 at 06:09