【发布时间】:2014-01-23 03:08:20
【问题描述】:
我知道到处都有教程,但由于某种原因我无法弄清楚。我有一个标签栏控制器。每个选项卡都链接到一个导航控制器,该控制器连接到一个视图控制器。因此,有 2 个主视图控制器(StatusVC 和 TransactionsVC)。
在 StatusVC 中,我有一个文本字段。在 TransVC 中,我有一个表格视图。一个人将一个单元格添加到表格中。数学是在幕后完成的。单元格值相加(数字)。此信息被发送回 StatVC 以进行数据计算和显示。我已经把数学部分写下来了。我的问题:如何在视图控制器之间传输数据,更好的是,如何存储这些数据以使其在退出时不会被删除(可能是 NSUserDefaults)?
我想这可以分解为数据的传输,数据的保存,以及按下选项卡并显示视图时的数据显示。
我希望这是有道理的。无论如何,这是我得到的代码。您正在查看 TranVC。用户使用警报视图将数据输入到表中。您正在查看部分警报视图委托方法。这是用户在单元格中输入数据(按完成)的时候。寻找带有 ******* cmets 的关键区域。
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];
}
对此的任何帮助都会令人惊叹。
【问题讨论】:
标签: ios objective-c nsuserdefaults transfer