将文本从一个视图输入到另一个视图 [英] Putting text input from one view to another
问题描述
我尝试了很多教程,我想知道为什么我没有遇到这么简单的问题.我有一个名为 SetBudgetViewController 的视图控制器.我在此视图中有一个文本字段,我已将其连接为名为 *amountToSpend 的出口.我在应用程序的其他地方使用了另一个视图,它有一个名为 *amountSet 的标签.如何使输入到第一个文本字段中的数字显示在另一个视图的标签中?非常感谢大家(这让我发疯)!
I have tried so many tutorials that I am wondering why I am not getting such a simple problem. I have a view controller called SetBudgetViewController. I have a text field in this view that I have connected as an outlet called *amountToSpend. I have another view used elsewhere in the app that has a label called *amountSet. How do I make the numbers entered into the first text field be displayed in the label in the other view? Thank you all so much (this is driving me mad)!
推荐答案
首先,在另一个视图控制器中声明一个属性:
First, declare a property in the other view controller:
@property (strong, nonatomic) NSString *amountToSpend;
在 SetBudgetViewController 中,在您的 -(void)prepareForSegue 方法中:
In SetBudgetViewController, in your -(void)prepareForSegue method:
if([segue.identifier isEqualToString:@"YourIdentifier"])
{
OtherViewController *vc = segue.destinationViewController;
vc.amountToSpend = self.amountToSpend.text;
}
在另一个视图控制器中,在 viewDidLoad 中显示金额.
In the other view controller, display the amount in viewDidLoad.
self.amountSet.text = self.amountToSpend;
编辑 2:在彼此不接近的 VC 之间传递数据的替代方法.您可以重复上述操作或使用 NSUserDefaults.
EDIT 2: Alternative for passing data between VCs not close to each other. You can repeat the action above or use NSUserDefaults.
在输入金额后在 SetBudgetViewController 中:
In SetBudgetViewController after amount is entered:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:self.amountToSpend.text forKey:@"AmountToSpend"];
[defaults synchronize];
在另一个视图控制器中,在 viewDidLoad 中显示金额.
In the other view controller, display the amount in viewDidLoad.
self.amountSet.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"AmountToSpend"];
这篇关于将文本从一个视图输入到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!