在UITabBarController视图之间传递数据 [英] Pass data between UITabBarController views

查看:80
本文介绍了在UITabBarController视图之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一整天的一个简单的例子并且还没有找到它。我正在开发一个应用程序,我想在初始加载时进行API调用,并填充一些可在任何选项卡视图中访问的变量。

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.

示例:我想进行单个API调用以获取我的数据,其中包括与警报选项卡的警报相关的数据。我想在初始加载时使用此数据来更新警报选项卡徽章。但是,一旦此人转到该选项卡,我也希望在警报页面上使用该信息,而无需再次进行API调用以获取相同的信息。

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已经阅读了大量不符合我要求的解释,所以我正在寻找一个很好的例子来帮助我。

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.

推荐答案

使用 UITabBarViewController viewControllers 属性访问选项卡栏控制器中的视图控制器数组。根据您的标签栏控制器架构对它们进行类推。
然后使用该数组获取指向任何视图控制器的指针。

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.

例如,假设您的标签栏控制器有5个标签,每个标签都有 UINavigationController 其中具有特定的 UIViewController 作为根视图控制器。假设您要将第3个选项卡的徽章值设置为Web响应阵列计数。你可以这样做

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];

这篇关于在UITabBarController视图之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆