I have a tab bar controller with two tabs. The first tab displays a table view of things of type A. If you click on a cell a view of type A is pushed onto a navigation stack. As you would expect, this view has a back button that pops the view from the navigation stack and displays the table view of things of type A.
The second tab displays a table view of things of type B. If you click on a cell a view of type B is pushed onto a navigation stack. Again, as you would expect, this view also has a back button that pops the view from the navigation stack and displays the table view of things of type B.
The type A view also has a button that conceptually converts the type A thing to a type B thing. Visually, the type A view is replaced with a view of type B. Behind the scenes, I need to pop the type A view off of the navigation stack, change tabs to the table things of type B and pop a type B view controller onto a navigation stack so that the type B view’s back button will take the user back to the table of things of type B.
My first idea for implementing this was to link the button on my type A view to my implementation of the tab bar controller, which pops off the existing type A view, changes tabs to the list of type B things and then pushes type B view onto the navigation stack. But this creates an otherwise unnecessary reference to the tab bar controller from the type A view.
Is there a better way to do this? Perhaps with a custom segue?
Since posting my question, I learnt that it isn’t necessary to maintain my own explicit reference to the tab bar controller because each UIViewController has the property:
parentViewController.This means that I can access my custom tab bar controller from my type A view without maintaining my own reference. As a result, my code is a bit simpler now. When the button on the view of type A is pressed the following code is executed in Type A’s view controller:
Where switchToViewOfTypeB is the code on the custom tab bar controller that performs the segue.
I like this approach better than using a notification in this case because it is clear from the type A view that it is the custom tab bar that is responsible for performing the segue.