I have a tab bar MainViewController with a few tab bar buttons, the first being View 1 which is a navigation controller with a stack of up to 3 or 4 children. The final tab bar button is SettingsViewController, with a function similar to ‘Logout’ within it, which calls a method on the AppDelegate which removes MainViewController and sends the user back to a login page.
When pressing the logout button on SettingsViewController I want the AppDelegate to set the selected tab to be View 1 (which I’ve done), send the user back to the logon view (also done) but also popToRootViewController on View 1 which is the bit I’m struggling with.
I’ve tried using the viewControllers property on the tabBar to grab a reference to the view controller at index 0 but that doesn’t appear to be the correct way unless I’m implementing it incorrectly.
My most recent attempt:
// Fine
mainViewcontroller.tabBarController.selectedIndex = 0;
// Not so fine
NSArray *allViewControllers = [mainViewcontroller.tabBarController viewControllers];
View1 *vc = [allViewControllers objectAtIndex:0];
[vc.navigationController popToRootViewControllerAnimated:NO];
Another attempt at just iterating through all view controllers in the tab and popping to the root which equally doesn’t seem to work:
for (UIViewController *viewController in mainViewcontroller.tabBarController.viewControllers)
{
[viewController.navigationController popToRootViewControllerAnimated:NO];
}
Can anyone help?
Just for closures sake… using exactly the same code I pasted in my question worked find when I recreated the tab bar controller programatically instead of in IB.