-1

I have a UITabBarController with Tabs (say Tab1, Tab2, Tab3, Tab4) and UITabBarController is my RootViewController and I'm making an API Call in the same. Since it is a RootViewController I'm displaying Tab1 as my default View. When I get the results to my API Call in UITabBarController. I need to share the details in real time to my Tabs. I have tried few ideas(like NotificationCenter, Singleton Class) but it's not working out. Can somebody help me to fix this? Thanks in advance. If you have a working example I kindly request you to share it with me.

Img Representation:

enter image description here

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Deepakraj Murugesan
  • 1,195
  • 11
  • 30

3 Answers3

4

UITabBarController has a property viewControllers that is an array of view controllers displayed (by index for tab position, but you probably don't need that). One simple solution is when your UITabBarController subclass gets data, it can loop over the viewControllers array and check each for delegate conformance / responds to selector and update accordingly.

That's probably the simplest. Another way would be to have the view controllers in the tabs register for updates. So they conform to a delegate protocol, and get a reference to their tab bar controller (the tab bar controller subclass), and call the tab bar saying "observe updates." The tab bar controller keeps storage of registered observing objects and calls each with new data.

This assumes you are setting the tab's view controllers in IB. If you are doing it programmatically, then with the second option you can just link the tabs to the tab bar controller when you add them. If set in IB you could also do it by overriding -prepareForSegue since embedding the tabs in the root is considered a segue, but you'd still have to cast the destinationViewController as whatever subclass can receive data.

The first option is simple enough and though I don't like casting, it's unavoidable to take advantage that the references you want are right there. Plus, there will in reality only ever be a single-digit number of tabs, so it can't get expensive. Hope this helps.

Mike Sand
  • 2,750
  • 14
  • 23
2

This is just same like passing the values to another viewController but here you need to use tabBarController's delegate method in below example:

Passing value using delegate method from controller A:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    ControllerB *controllerB = (ControllerB *) [tabBarController.viewControllers objectAtIndex:1];
    //In this example, there is only have 2 tab bar controllers (A and B)
    //So, index 1 is where controller B resides and index 0 where controller A resides.

    self.controllerB.data.text = @"some value!";
    //This will change the text of the label in controller B
} 

Getting value in controller B

// set property in controller B .h file
@property(strong, nonatomic) UILabel *data;

// in controller B .m file viewDidLoad method 
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"getting data: %@",data.text); // getting value 
}
vaibhav
  • 4,038
  • 1
  • 21
  • 51
  • Thanks Vaibhav !! But my prob is !! First index of the tab is visible to the user and when the response is success on the tab bar controller. The data should come to first index of the tab and for rest of the tabs I will pass like the way you said me. How will I be able to know that the response is received in UITabbarController. – Deepakraj Murugesan Jan 27 '17 at 15:06
  • @DeepakrajMurugesan i don't know how you getting data from server but using my method you will be able to reflect the data to the corresponding classes of `UITabbarController`, so that's the ans of you ques. For response i guess you must be using some connection delegates method. – vaibhav Jan 28 '17 at 06:58
1

How about

  • -Declare a delegate protocol, say, Tab1ViewControllerDelegate, with a method - (void) tab1ViewController:(Tab1ViewController *) tab1ViewController, didReceiveData: (NSDictionary *) data

  • make a TabBarController subclass a delegate of your tab1ViewController

  • when tab1ViewController gets it's data, call [self.delegate tab1ViewController: self, didReceiveData: datDict]

  • then your TabBarController can distribute the data to its array of viewControllers

Ashley Mills
  • 50,474
  • 16
  • 129
  • 160