1

I've used search already, haven't found an answer.

Trying to switch like this:

[self. tabBarController.selectedViewController OptionsViewContorller];

and like this:

 [self.tabBarController.selectedViewController = self.tabBarController.viewControllers     objectAtIndex:3];

but it doesn't work, i also tried and advice to change

self.tabBarController.selectedIndex

but it only changes at tab bar not a view.

Horhe Garcia
  • 882
  • 1
  • 13
  • 28

5 Answers5

6

This should work.

self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:3];
Wubao Li
  • 1,728
  • 10
  • 13
  • it just changes selected icon it tab bar but it doesn't change view. – Horhe Garcia Mar 09 '12 at 12:00
  • @HorheGarcia is the icon changed to correct. if the icon and the tab index is right, check the selected view controller. Does you tap the tab index, it behaves right? – Wubao Li Mar 09 '12 at 13:28
  • i found the problem, my tab tab is situated in another controller. From that controller it works ok, how can i access it from another controller? – Horhe Garcia Mar 10 '12 at 09:34
  • @HorheGarcia use delegation pattern to interact between controllers – Wubao Li Mar 10 '12 at 16:42
2

// this code i am using to switch to tabbar view controller 0, first view controller.

self.tabBarController.selectedIndex = 0;

UIViewController *controller = [self.tabBarController.viewControllers objectAtIndex:0];
if ([controller isKindOfClass:[UINavigationController class]]) {
    [((UINavigationController*)controller) popToRootViewControllerAnimated:false];
}

[self.navigationController popToRootViewControllerAnimated:true];
Tajen
  • 21
  • 1
0

If you want to switch from your UITabBarController class, you have to write this code in -viewDidAppear:animated:

[((UIViewController *) self.viewControllers[0]).tabBarController setSelectedIndex:1];

Hope this help.

sancho
  • 443
  • 4
  • 14
0

for swift 4+

you have to get reference to the tab before the view was presented

let tab = self.presentingViewController as! UITabBarController
self.dismiss(animated: true, completion:{ 
     tab.selectedIndex = 2
})
Johnyoat
  • 436
  • 4
  • 14
0
// viewControllerIndex is an int describing the position of the viewController
// in the tab bar array index
[self.tabBarController setSelectedIndex:viewControllerIndex];
Damo
  • 12,840
  • 3
  • 51
  • 62
  • it just changes selected icon it tab bar but it doesn't change view. – Horhe Garcia Mar 09 '12 at 11:51
  • If the tab bar icon is changing it sounds like it IS changing the view controller - are you sure the views in your tab bar are **distinct** enough to recognise them changing? – Damo Mar 09 '12 at 12:02
  • they are absolutely different. why it may be so? icon becomes active but views doesn't change.. – Horhe Garcia Mar 09 '12 at 12:08
  • its best if you refer to Tabster, included with ios documentation. there must be something you are doing wrong. – JOA80 Mar 09 '12 at 12:13
  • i found the problem, my tab tab is situated in another controller. From that controller it works ok, how can i access it from another controller? – Horhe Garcia Mar 10 '12 at 09:33