在视图控制器之间传输数据的新方法 [英] New ways to transfer data between view controllers

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

问题描述

我在 UIViewController 之间传输数据时遇到问题,我在之前的问题中找不到答案.

I have a problem with transferring data between UIViewController and I don't find answers in former questions.

情况:我有一个允许用户输入数据的第一个视图控制器('CalViewController').然后我用这些数据计算一个数字(例如名为卡路里").接下来的视图是两个 UIViewController (DrinksViewController & FoodViewController) 显示在 TabBarController 中,我需要 '卡路里'.

Situation : I have a first viewController ('CalViewController') which allows users to input data. Then I calculate with those datas a number (named 'calories' for example). The next views are two UIViewController (DrinksViewController & FoodViewController)displayed in a TabBarControllerand I need value of 'calories'.

我尝试过的:-prepareForSegue 方法:它不起作用,因为 TabBarController 中的 segues(故事板中的符号是两点之间的链接)不像其他的(故事板中的符号是箭头通过一扇门).

What I've tried : -prepareForSegue method : It doesn't work because segues (symbol in storyboard is a link between two points) in a TabBarControllerare not as others (symbol in storyboard is arrow through a door).

-'didSelectViewController' 方法:此方法未激活"以显示 TabBarController 的第一个视图.所以我成功地将 Calories 传输到 TabBarController 中的第二个 ViewController(即 FoodViewController),但没有传输到 TabBarController 中的第一个 viewController(即 DrinksViewController).

-'didSelectViewController' method : This method is not "activated" to display the first view of a TabBarController. So I succeed to transfer Calories to my second ViewController in the TabBarController (ie FoodViewController) but not to my first viewController in the TabBarController (ie DrinksViewController).

-调用原始"值:这里是我在 CalViewController 中所做的(导入 DrinksViewController.h 后)

-call the "original" value : Here what I've done in CalViewController (after imported DrinksViewController.h)

DrinksViewController *dvc = [[DrinksViewController alloc] init]; 
dvc.caloriesImported = 456;

我不知道为什么这第三种方法不起作用.

I don't know why this third way is not working.

问题:我的卡路里值没有从CalViewController转移到DrinksViewControlller.有什么想法吗?

Problem : My value of Caloriesis not transferred from CalViewController to DrinksViewControlller. Any ideas ?

推荐答案

如果我正确理解您的问题,以下应该首先起作用视图控制器 CalViewController:

If I understand your problem correctly, the following should work in the first view controller CalViewController:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"yourSegueToTabBarIdentifier"]) {
        UITabBarController *tbc = [segue destinationViewController];
        DrinksViewController *dvc = tbc.viewControllers[0];
        dvc.caloriesImported = 456;
        FoodViewController *fvc = tbc.viewControllers[1];
        fvc.someProperty = someValue;
    }
}

注意

DrinksViewController *dvc = [[DrinksViewController alloc] init]; 
dvc.caloriesImported = 456;

无法工作,因为它分配了一个 DrinksViewControllernew 实例与标签栏控制器使用的实例完全无关.

cannot work because it allocates a new instance of DrinksViewController that is completely unrelated to the instance used by the tab bar controller.

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

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