0

I am using popToViewController method to pop to a view controller. What I was trying to achieve is to pop to an UITabBarController's specific index.

NSArray *viewArrays = [self.navigationController viewControllers];

So my view hierarchy is;

<LoginViewController: 0x6b75850>,
<UITabBarController: 0x6ba0b50>,
<RequestViewController: 0x684fe90>,
<ApplyViewController: 0x6845790>

Following code does pops to my UITabBarController, but since I was on the third view of my UITabBarController, it pops back to the third view

[[self navigationController] popToViewController:[self.navigationController.viewControllers objectAtIndex:1]  animated:YES];

What I want is to pop to the second view of my UITabBarController.

Edit (Answer)

As stated, we don't push or pop. So in order to gather your tabBarController you either use;

UITabBarController *myTabController = [self.navigationController.viewControllers objectAtIndex:indexOfYourTabBarControllerInYourNavigationController];
myTabController.selectedIndex = indexToGo;

or if you are on one of your tabBarController views;

self.tabBarController.selectedIndex = indexToGo;
Bartu
  • 2,189
  • 2
  • 26
  • 50

1 Answers1

2

In UITabBarController you dont pop and push, if you want to go to the second view you would set the selected index instead

Like so

yourTabController.selectedIndex = 1;

Or if the current view controller is a part of the tabBarController do

self.tabBarController.selectedIndex = 1;
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
  • The second example can't work - selectedIndex is a `NSUInteger` property (and as such it can't be used as a `tabBarController`. Even if you'd use property `selectedViewController` you should typecast it to `TabBarController *` - and doing this you'd have to be certain that it really IS a `TabBarController`... – Rok Jarc Jun 24 '12 at 13:14
  • and how can I add my tabBarController to my current ViewController ? to use myTabcontroller.selectedIndex ? second one does not work as stated... – Bartu Jun 24 '12 at 13:21
  • UITabBarController *myTabController = [self.navigationController.viewControllers objectAtIndex:1]; myTabController.selectedIndex = 2; this code segment doesn't seem to work to get my tabBarController, at least I cant set my selectedIndex – Bartu Jun 24 '12 at 13:31
  • instead use UITabBarController *myTabController = self.tabBarController; – Omar Abdelhafith Jun 24 '12 at 13:40
  • it does not work. Im not currently on my tabbarcontroller views, as you can see I am at ApplyViewController, which is index 3 on my navigationController stack, my TabBarController index is 1. Can i reach my tabTarcontroller with self.tabBarController ? – Bartu Jun 24 '12 at 13:49
  • @Bartu please post some code on how you presented ApplyViewController – Omar Abdelhafith Jun 24 '12 at 13:51
  • 4th comment of mine was working, I confused my index with 2, I should have been doing 1 instead. Thanks for your help sir. – Bartu Jun 24 '12 at 13:53