Navigate back from a UIViewController to its closest parent

I have a UITabBarViewController that is the rootViewController of the UIWindow.

The tabBarViewController contains a UIViewController; lets call it HomeViewController. HomeViewController contains a UIViewController called ListViewController. ListViewController contains a UIViewController called ItemViewController
Code Block
UIWindow
HomeViewController
ListViewController
ItemViewController

The UITabBar is always visible.

Pressing the Home tab returns to HomeViewController even if I am in ItemViewController. I do not want this. I want to navigate to ListViewController and then to HomeViewController should I press the Home tab bar again.


How to navigate back from a UIViewController to its closest parent? In this case, How to return from ItemViewController to ListViewController if you press the Home tab bar once?

Replies

You can set the delegate of the UITabBarController and respond to the didSelect method. Or you can subclass UITabBarController and override whatever methods you need.

Personally I’d probably try using the delegate and broadcast a notification that the selected tab had changed. Then your child view controller could register for that notification and call popViewController:animated: in response.

Edit: I would caution you against having a nonstandard use of a tab bar. Users expect that when they tap on a tab bar icon it returns them to that tab the way it was when they left it, with no side effects. You should not have different behaviours if you tap the same icon again. It creates an uncomfortable feeling when your app behaves differently from others on the platform. Might even be a HIG violation; I don’t know.
Home is Home, so it sounds logical that it returns you to home.
You are using tab, not navigation. What you describe is the expected behaviour of a navController, not a tabBarController.

IMHO it would be misleading for user to return just one step back and not to the Home.

What you want is a back button in Navigation.