views:

514

answers:

1

Here's the setup:

I have a tab bar controller with two tabs. There is a navigation controller on top the second tab, so that I can view details from a table.

On tab #1, I have a button. When this button is pressed, it switches the selected tab over to tab #2.

My problem is this: Let's say I go to tab #2, then I select a line from my table to view the detailed information. Then, instead of hitting the "back" button to return to the base view, I select tab#1. Then I press my button. Tab#2 loads, but it is still showing the detailed view, instead of the table view that I want.

I know how to programmatically press the button.
[self.navigationController popViewControllerAnimated:YES];

But how would I do that when the button pressed method is inside the class for tab#1, which knows nothing of tab#2's navigation controller?

Hope that makes sense. Thanks in advance for any help!


Okay, I finally got it to work. The solution that was suggested gave me a good jumping off point. Here is the code that finally worked for me:

ARFinderAppDelegate appDelegate = (ARFinderAppDelegate)[[UIApplication sharedApplication] delegate];

[appDelegate.booksNavigationController popViewControllerAnimated:YES]; self.tabBarController.selectedIndex = 1;


I had to access the class that contained the NavigationController through an instance of my application delegate. I also had to remove the "objectAtIndex:1" because my application would crash.

Thanks for all the help, this has been a great learning experience for this iPhone development newbie!

+2  A: 

use your delegate singleton.

[[delegate.tabBarController.viewControllers objectAtIndex:tab2Index] popViewControllerAnimated:YES];
Jesse Naugher
"delegate" does not appear to be a keyword, and I get an error that its undefined. If I try using "self.delegate" instead, I get another error: Request for member 'delegate' is something not a structure or union. I know I must be missing something simple, I'm new to all of this. Could you help me out please? Thanks!
IcyBlueRose
your app delegate is what i am referring to. Insert the name of your appdelegate file where "YourAppDelegate" is.. (this is the file with applicationDidFinishLaunching: in it and probably the logic for your tab bar controller.YourAppDelegate *delegate = [[UIApplication sharedApplication] delegate];in front of the line given above.
Jesse Naugher