Search code examples
iosuitabbarcontrolleruitabbar

Pass a boolean to all tabs in a UITabController


In my app, there is UITabBarController that has 3 TabBar. TabBarController is called from ViewController that has a boolean variable in it.

I want to pass this value from UIViewController to UITabBarController (in -prepareForSegue, I assume) so that I can get this value in all the tabs of TabBarController.

How can I pass this variable and how can I access the from each TabBar?


Solution

  • One way of solving it will be to use delegates. You can extend UITabBarController with your own class and create a delegate methods to communicate with UIViewController. Than in prepareForSegue method you can get boolean on demand using this method.

    If you never used it before, here is a nice read about Protocol and delegation: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithProtocols/WorkingwithProtocols.html

    Another idea will be to again extend UITabBarController and add BOOL property to it and update it every single time from UIViewController. The best way will be to create custom setter in your UIViewController that updates according property in UITabBarController. In this case you can just use self.boolproperty in UITabBar segue.

    But if you want to do it in Obj-C style - delegate is the way to go.