Reputation: 363
I have a TabViewController setup in my iOS app with 4 tabs. Some of the tabs go to static ViewControllers while a few go to a navigation controller that the user can then dive deeper into.
My problem is this, in the app delegate, I implement the "didReceiveRemoteNotification" method, when I receive a remote notification I display a drop down notification using a library similar to this https://github.com/terryworona/TWMessageBarManager
I would like the simplest and cleanest way to add a callback to one of these drop down messages such that when clicked I display a particular index in the tab view regardless of where I am in the app.
I have thought of possibly having the app delegate broadcast a notification that the currently displayed view controller would listen too, then have the currently displayed view controller unwind if it is nested in a navigation controller to the top level, i.e. the level where the TabViewController would be displayed and then change the tab view controller's index. Or if the TabViewController was currently displayed, it would capture the notification and simply change index. But this seems conceptually intrusive because now my ViewControllers themselves must have knowledge about where they are in the UI hierarchy, and all ViewControllers (which there are many) must implement functionality to deal with the notification so they can decide to unwind + set tab index OR simply set tab index.
Upvotes: 0
Views: 291
Reputation: 321
I would subclass the TabBarController, and you could add a method like -(void) receiveMessageBarNotification:(NSNotification*)notification. The TabBarController already has references to your view controllers and so can handle all the logic of 'navigation controller stack vs static VC'. Then it would call the unwind segue on the view controller (if necessary) and change its own index.
The notification registration could all be handled by the AppDelegate.
Upvotes: 1