1
votes

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

3

3 Answers

7
votes

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];
}
1
votes

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 ..

1
votes

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.