How do I switch between tabs and pushViewController at the same time?
I have two tabs in my application, the left tab is a regular UIViewController, the right tab is a navigation controller with a table view inside it. I would like the user to click a button on the left tab and go to a specific detail view that belongs to a specific row in the table view on the right tab.
In the table view itself, I have no problem displaying a specific detailed view, for example:
[self.navigationController pushViewController:theView animated:YES];
and i can also jump tabs using
self.tabBarController.selectedIndex = 1; // 1= right tab
but I can't figure out how to do it. I tried
self.tabBarController.selectedIndex = 1; // 1= right tab
// alloc and init theView here
[self.navigationController pushViewController:theView animated:YES];
but it doesn't work.
EDIT:
when i try the above code it just switches the tab but doesn't click the view.
I have also tried what criskokid suggested, but nothing happens at all.
EDIT 2:
Trying to explain what's in each tab:
Root (Tab Bar Controller) | - (Left Tab) - UIViewController (I want to go from here ...) | - (Right Tab) --- UINavigationController | --- UITableViewController | ---- UIViewController (... to here) ^ specific view with data from specific row in table view (that is 'theView' from above)
The correct way for me is:
UINavigationController * navigationController = (UINavigationController *) [[self tabBarController] selectedViewController];
[navigationController pushViewController:viewController animated:YES];
Criscokid's answer has one redundant call navigationController
. This is because yours selectedViewController
really is UINavigationViewController
.
source to share
If you switch the tab bar and push the ViewController onto the navigation controller stack in the same way, you won't want to use self.navigationController. If I understand correctly, you want to add it to the navigation controller on the right tab. I believe you would like to use:
[[[[self tabBarController] selectedViewController] navigationController] pushViewController:theView animated:YES];
source to share