1

i am having application consisting of both TabBarController and Navigation Controller.I am having two tabs as tabA and tabB. The default selecetd tab is 1(tabB) with view controller L(when the application finish its launching).Now user can navigate as :L=>M=>N=>O. View controller for tabA is x.user can navigate as:x=>y=>z. there is a button on the top of the z view controller which should navigate/push/pop consumer to root view controller of tabB with view controller L.

Any sugestions,thanks

maddy
  • 4,001
  • 8
  • 42
  • 65

3 Answers3

7

This shouldn't be to hard, if I understand your question correctly that is.

The method should look something like this:

- (void) pop
{
    UIViewController * target = [[self.tabBarController viewControllers] objectAtIndex:1];

    [target.navigationController popToRootViewControllerAnimated: NO];

    [self.tabBarController setSelectedIndex:1];
}
Peter Sarnowski
  • 11,900
  • 5
  • 36
  • 33
  • +1 :Use this answer ... you have been just few second faster then me :) – Jhaliya - Praveen Sharma Feb 14 '12 at 15:09
  • Uuuhh I hate when that happens. Sorry :) – Peter Sarnowski Feb 14 '12 at 15:10
  • thanks,i have been able to pop to root view controller but it always pops to the last navigation item in the view controller of tabB.Means, if consumer navigate from :L=>M=>N=>O in tabB,now leaves tabB whereVer it is(either L/M/N/o).Now from tabA if i try to POPING THE root view controller of tabB it pops to where he left the navigation in tabB(either L/M/N/O).i want to navigate him to L only with selected tab 1(selected tab is working fine) . – maddy Feb 14 '12 at 15:49
  • It is kind of difficult to tell exactly how to do that without knowing your implementation details, but you should be able to easily achieve what you need using the tabbarcontroller and navigationcontroller properties of your viewcontrollers. – Peter Sarnowski Feb 14 '12 at 16:25
  • @PeterSarnowski there was some context problem.i have to do some thing like this: in appdelegate file :UIViewController * target, set property (nonatomic,retain) and synthesize it.Now in the viewController(rootController) viewDidLoad i do as:UIAppdelegate.target=self;.A small change in Your code as:[UIAppdelegate.target.navigationController popToRootViewControllerAnimated: NO]; [UIAppdelegate.tabBarController setSelectedIndex:1]; [self.tabBarController setSelectedIndex:1]; – maddy Feb 16 '12 at 16:57
  • But in this way there is no animation which seems strange – xi.lin Jan 23 '16 at 07:43
1

I had a similar issue, but I think I found a much simpler way to deal with it. In the view controllers that the user might end up on (O or Z in initial question), I put

    [self.navigationController popToRootViewControllerAnimated:NO];

In the viewWillDisappear of those VCs. Seems straight forward and easy. Am I missing a reason why I wouldn't want to do this? Like the original questioner, I always want to start at the root VC of my tab regardless of where the user left off the last time they were on that tab.

Quick update: I constructed my own version of a split view controller and adding the above line to viewWillDisappear did some undesirable things on an iPad, so I had to add a condition to check for device type (which wasn't affected by the tab bar issue anyway because both view controllers were part of the splitVC I constructed) but otherwise seems to do what I want.

MikeMilzz
  • 79
  • 8
1

You need to pop to the root of the current tabbar then use the tabbar controller method to select the tab bar button using setSelectedIndex method ..

Jhaliya - Praveen Sharma
  • 31,697
  • 9
  • 72
  • 76