Transferring data between UITabBarController views

I searched all day for a simple example about this and haven't found it yet. I am working on an application, and I want to make a bootstrap API call and populate some variables that will be available in any of the tab types.

Example. I want to make one API call to get my data, which will include data related to alerts for the alert tab. I want to use this data on boot to update the Alerts tab icon. However, I also want to use this information on the warning page as soon as a person goes to this tab, without having to repeat another API call to get the same information again.

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

+4
source share
5 answers

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

For example, your tab bar controller has 5 tabs, each tab has a UINavigationController that has a specific UIViewController as root view controllers. Suppose you want to set the value of the 3rd tab icon to your array of response arrays. You can do it like

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

You can also go to a specific property of the visibility manager by specifying control controllers. for instance

 MyViewController *myVc = (MyViewController*) [[(UINavigationController*)[[self.tabviewController viewControllers] objectAtIndex:2] viewControllers] objectAtIndex:0]; [myVc setPropertyName:propertyValue]; 
+15
source

I asked this question from yesterday and made sure that it needs to be looked for before publication. It didn't seem like I found the answer to this question, and it might be very straightforward or maybe this is not the way to do it, but here is how I solved this problem: using NSUserDefaults and the sample code on this page

+2
source

Put all these variables in one class and get access to the shared instance whenever you want. Add

 + (YourClass *)sharedObject { static YourClass *sharedClassObject = nil; if (sharedClassObject == nil) { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedClassObject = [[YourClass alloc] init]; //Initialise it here if necessary }); } return sharedClassObject; } 

To access the shared instance, just use [YourClass sharedObject] .

+2
source

Put the data in the application delegation object. You can access it from anywhere in your application using (MyAppDelegate *)[[UIApplication sharedApplication] delegate] , or you can give each of your view controllers an explicit link to it.

NSUserDefaults is not really designed to share data globally in your application, although it does the job. It also has the advantage that information is stored if your application cannot connect to the server next time. Is this good or bad?

+1
source

You must use the NSNotificationCenter to publish notifications of new data and new data as an object.

Each of the viewControllers that need this object should just subscribe to this notification and just consume new data.

0
source

Source: https://habr.com/ru/post/1485155/


All Articles