Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass data between UITabBarController views

I have searched for an entire day for a simple example on this and haven't found it yet. I am working on an app and I want to make an API call on the initial load and populate some variables that will be accessible in any of the tab views.

Example: I want to make a single API call to get my data which will include data relating to alerts for the alerts tab. I want to use this data on the initial load to update the "Alerts" tab badge. However, I want to also use that information on the alerts page once the person goes to that tab without having to make another API call to get the same information again.

I have read a ton of explanations that do not fit my requirements, so I am looking for a good example to help me out.

like image 409
Donovan Avatar asked Dec 01 '22 21:12

Donovan


1 Answers

Use your UITabBarViewController's viewControllers property to access the array of view controllers in your tab bar controller. Typecast them according to your architecture of tab bar controller. Then get a pointer to any of view controller using that array.

For example, say your tab bar controller has 5 tabs, each tab having a UINavigationController which has particular UIViewController as root view controllers. Say you want to set badge value of 3rd tab to your web response array count. You can do that as

[[[self.tabviewController viewControllers] objectAtIndex:2] 
        setBadgeValue:[NSString stringWithFormat:@"%d",[myArray count]];

You can also get to particular view controller's property by typecasting the view controllers. For example

MyViewController *myVc = (MyViewController*) [[(UINavigationController*)[[self.tabviewController viewControllers] objectAtIndex:2] viewControllers] objectAtIndex:0];
[myVc setPropertyName:propertyValue];
like image 59
Zen Avatar answered Dec 09 '22 09:12

Zen