Pass data between UITabBarController views

2020-03-04 08:47发布

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.

5条回答
虎瘦雄心在
2楼-- · 2020-03-04 08:47

I had this question typed up since yesterday and made sure to search before posting. There was no question similar that I found that had the answer, and it may be very straight forward or maybe this is not the way to do it but here is how I solved this issue: using NSUserDefaults and the code example on this page

查看更多
倾城 Initia
3楼-- · 2020-03-04 08:47

You should use NSNotificationCenter to post the notification that new data arrived and your new data as an object.

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

查看更多
老娘就宠你
4楼-- · 2020-03-04 08:56

Put all of those variables in a single class and access a shared instance of it 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, simply use [YourClass sharedObject].

查看更多
叛逆
5楼-- · 2020-03-04 09:04

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];
查看更多
疯言疯语
6楼-- · 2020-03-04 09:07

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

NSUserDefaults isn't really meant for sharing data globally in your app, although it would get the job done. It also has the benefit that the information sticks around if your app can't connect to the server next time. Is that a good thing or a bad thing?

查看更多
登录 后发表回答