在 TabBarViewController 的选项卡之间传递数据 [英] Passing data between the Tabs of a TabBarViewController
问题描述
我有一个基于 UITabBarController 的应用程序,我想将数据从一个视图传递到另一个视图.我正在故事板中执行此操作,我只是在将其引入主应用程序之前进行一些测试.
I have a UITabBarController based application and I want to pass data from one view to another. I am doing this in storyboard and I am just doing some testing, before bringing it into the main application.
我现在只是用 NSString 尝试这个.
I am just trying this with a NSString at the moment.
当我使用以下代码使用模态转换时,我能够将数据传递给有问题的 VC:
I am able to pass data to the VC in question when I use a modal transition using this code:
NSString *sendingString = @"This string has some content";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
NextViewController *nVC = (NextViewController *)[storyboard instantiateViewControllerWithIdentifier:@"goToNextVC"];
nVC.receivingString = sendingString;
[self presentViewController:nVC animated:YES completion:nil];
现在这会推动该 VC 并根据我的需要传递它,但我希望将其推送到另一个标签栏而不是推动 VC.
Now this pushes that VC up and passes it as I want it, but instead of pushing up the VC I want it to be pushed to another Tab Bar.
现在我可以使用以下代码滑动到所需的 TabBar:
Now I can flick to the desired TabBar with this code:
self.tabBarController.selectedIndex = 1;
我卡住的地方是,我如何向这个 ViewController 发送数据???
Where I get stuck is, how do I send data to this ViewController???
推荐答案
您可以子类化 TabBarController
,并为其添加属性,或者创建一个单例(例如 DataManager),所有您的 ViewControllers
将有权访问.您可以将数据传递给它.
You could either subclass your TabBarController
, and add a property to it, or create a singleton (e.g. DataManager), to which all your ViewControllers
will have access. You can pass your data to it.
这篇关于在 TabBarViewController 的选项卡之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!