我知道到处都有教程,但是由于某种原因我无法弄清楚。我有一个标签栏控制器。每个选项卡都链接到导航控制器,该导航控制器固定在视图控制器中。因此,有2个主视图控制器(StatusVC和TransactionsVC)。

在StatusVC中,我有一个文本字段。在TransVC中,我有一个表格视图。一个人将一个单元格添加到表中。数学在幕后完成。单元格值相加(数字)。该信息被发送回StatVC进行计算和数据显示。我已经把数学部分放下来了。我的问题:如何在视图控制器之间传输数据,更好的是,如何存储此数据,以使其在退出时不会被删除(可能是NSUserDefaults)?

我想这可以分解,当按下标签并显示视图时,数据的传输,数据的保存和数据的显示。

我希望这是有道理的。无论如何,这是我得到的代码。您正在查看TranVC。用户使用警报视图将数据输入到表中。您正在查看Alert View委托方法的一部分。这是用户将数据输入到单元格中(按完成)的时间。使用*******注释查找关键区域。

StatusViewController *statVC = [[StatusViewController alloc]init]; //*******init

            // Set the amount left in the budget
            NSString *amountToSpend = statVC.amountLeftInBudget.text;
            double budgetLabel = [amountToSpend doubleValue];

            NSString *lastItem = [transactions objectAtIndex:0];
            double lastLabel = [lastItem doubleValue];

            double totalValue = budgetLabel - lastLabel;

            NSString *amountToSpendTotal = [NSString stringWithFormat: @"%.2f", totalValue];

            statVC.amountLeftInBudget.text = amountToSpendTotal; //*******set text (but not save), either way, this doesn't work

            // Set the amount spent
            NSString *sum = [transactions valueForKeyPath:@"@sum.self"];
            double sumLabel = [sum doubleValue];

            NSString *finalSum = [NSString stringWithFormat:@"%.2f", sumLabel];

            //Set the amountSpent label
            statVC.amountSpent.text = finalSum;  //*******set text (but not save), either way, this doesn't work


            // The maxed out budget section
            if ([statVC.amountLeftInBudget.text isEqualToString: @"0.00"]) //*******set color (but not save), either way, this doesn't work

            {
                statVC.amountLeftInBudget.textColor = statVC.currencyLabel.textColor = [UIColor redColor];
            } else if ([statVC.amountLeftInBudget.text compare:@"0.00"] == NSOrderedAscending)
            {
                statVC.amountLeftInBudget.textColor = statVC.currencyLabel.textColor = [UIColor redColor];
            } else if ([statVC.amountLeftInBudget.text compare:@"0.00"] == NSOrderedDescending)
            {
                statVC.amountLeftInBudget.textColor = statVC.currencyLabel.textColor = [UIColor colorWithRed:23.0/255.0 green:143.0/255.0 blue:9.0/255.0 alpha:1.0];
            }

            if ([statVC.amountLeftInBudget.text compare:@"0.00"] == NSOrderedAscending)
            {
                // Create our Installation query
                UIAlertView *exceed;
                exceed = [[UIAlertView alloc]
                          initWithTitle: @"Budget Exceeded"
                          message: @"You have exceeded your budget amount"
                          delegate: self
                          cancelButtonTitle: @"Okay"
                          otherButtonTitles: nil];
                [exceed show];
            }


任何帮助都将是惊人的。

最佳答案

这确实是一个普遍的问题。

有各种解决方案。我建议的一种是使用数据容器单例。用Google搜索Objective C中的单例设计模式。您甚至可以在SO上找到它的示例。

使用要共享的值的属性创建一个单例。然后教您的单身人士保存数据。您可以使用用户默认值,也可以使用NSCoding,可以将数据提取到字典中并将其保存到documents目录中的plist文件中,或者也可以使用其他各种方案。

关于ios - 变量和 View Controller 之间的数据传输,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21298591/

10-14 22:35
查看更多