gpt4 book ai didi

ios - 将数据从 2 个 View Controller 发送到一个 View Controller

转载 作者:行者123 更新时间:2023-11-29 03:21:43 25 4
gpt4 key购买 nike

我正在编写一个卡路里追踪器应用程序,其中有 3 个 View Controller 。它们都连接到 Storyboard中的标签栏 Controller 。

  • 节食 View Controller
  • 练习 View Controller
  • 主页 View Controller

基本上我想做的是从我的锻炼 View Controller 和我的饮食 View Controller 中获取数据并将其显示在我的主视图 Controller 上。这是我的 HomeViewController.m 中 viewDidLoad 的代码

    //Referencing both view controllers
MainViewController *mainViewController = [[MainViewController alloc] init];
ExerciseViewController *exerciseViewController = [[ExerciseViewController alloc] init];

//Doing the math
int totalCalsConsumed = mainViewController.totalLabel.text.intValue;
int totalCalsBurned = exerciseViewController.totalCalsBurned.text.intValue;
int totalAmountOfCalories = totalCalsConsumed - totalCalsBurned;

//display the data
NSString *totalAmtCalsText = [NSString stringWithFormat:@"%i", totalAmountOfCalories];
totalAmtOfCals.text = totalAmtCalsText;

此外,我无法使用 segues 传递任何数据,因为我所有的 View Controller 都连接到标签栏,并且没有用于标签栏的 prepareForSegue 方法。

感谢所有帮助,我也想知道我是否迫切需要使用 Core Date 来解决这个难题。现在我正在尝试避开 Core Date,因为它是一个非常高级的主题,我将在未来触及,但如果我必须为此应用程序使用 Core Data,我会想出一些办法。

最佳答案

由于您强烈希望避免核心数据——我认为应该考虑这一点——我可以提供一个快速的、也许是肮脏的(也许不是)解决方案,以便您可以通过 viewController 访问数据。

根据您正在做的事情,您似乎想从标签栏 View Controller 访问数据。

这样做的一种方法是在一个地方有一个数据模型,其他 View Controller 可以访问它。就像带有网络服务的服务器或具有将您的数据保存在一个地方的单例。

在你的情况下,你想要一个快速的解决方案,你现在可以这样做,只要提到的所有 ViewControllers 都在你的 tabBar 中

//Create variables
int totalCalsConsumed;
int totalCalsBurned;
int totalAmountOfCalories;

//All your view controllers are accessible from your tabBar like so
NSArray *myViewControllers = self.tabBarController.viewControllers;

//We iterate through the view controllers in the tabBar
for(int i = 0; i < [myViewControllers count]; i++){
//This is the current view controller in the for loop execution
id currentVC = [myViewControllers objectAtIndex:i];

//Check if the currentVC variable is of type MainViewController
if([currentVC isKindOfClass:[MainViewController class]]{
//lets access the totalLabel property and store in variable
totalCalsConsumed = ((MainViewController *)currentVC).totalLabel.text.intValue;
}
//Check if the currentVC variable is of type ExerciseViewController
else if([currentVC isKindOfClass:[ExerciseViewController class]]){
//if so lets now access the totalCalsBurned property and store in variable
totalCalsBurned = ((ExerciseViewController *)currentVC).totalCalsBurned.text.intValue;
}
}

//Doing the math
totalAmountOfCalories = totalCalsConsumed - totalCalsBurned;

//display the data
NSString *totalAmtCalsText = [NSString stringWithFormat:@"%d", totalAmountOfCalories];
totalAmtOfCals.text = totalAmtCalsText;

我刚写这篇文章时还没有测试过它,但从一开始它就应该很好。代码注释可让您了解发生了什么。非常简单。

在您开发健身应用程序后给您的小贴士将来,我确实会推荐一些更持久的东西,如果应用程序退出,数据不会丢失,特别是因为你正在创建一个健身应用程序。我目前在个人时间使用 BodyBuilding 健身应用程序,并且有时在我的锻炼过程中丢失数据(设置信息,每组总重复次数的练习)时感到沮丧,只是因为开发人员从不费心存储我的数据在我锻炼时的某个地方,以防我的电池没电了。

关于ios - 将数据从 2 个 View Controller 发送到一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21009242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com