iPhone将NSUserDefaults输入到UITextField中 [英] iPhone inputting NSUserDefaults into a UITextField

查看:53
本文介绍了iPhone将NSUserDefaults输入到UITextField中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,用户在该程序中第一次运行该程序-他们将必须填写大约10个不同的UITextField.我正在尝试保存字段,以便在程序的后续运行中,他们先前放置的内容将已经显示在那些UITextField中,因此不必重新输入它-除非他们想要编辑某些内容,否则他们仍然可以该选项.我认为我已经找到了一种使用NSUserDefaults保存字符串的好方法,但是现在我试图弄清楚如何使这些字段填充UITextField-似乎不像UILabels那样容易.这是我尝试的路线:

I am writing a program where I where the first time a user runs it - they will have to fill out about 10 different UITextFields. I am trying to save the fields so that on subsequent runs of the program that whatever they previously put will already be displayed in those UITextFields so the wont have to re-input it in - unless they want to edit something in which case they still have that option. I think that I have figured out a good way to save the strings using NSUserDefaults but now I am trying to figure out how to have those fields populate a UITextField - it doesnt seem as easy as if they were UILabels. This is the route I am attempting:

// in the viewDidLoad portion.
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults]; //Hooks.
NSString *placeHolderName = [userData stringForKey:@"name"];

txtName.text = @"%@", placeHolderName;

执行此操作时,它仅在文本字段中显示%@".我想把placeHolderName持有的任何变量自动放入该UITextField中.这可能吗?

When I do this, it simply displays the '%@' in the textfields. I want whatever variable being held by placeHolderName to be automatically put into that UITextField. Is this possible?

推荐答案

只需使用:

txtName.text = placeHolderName;

txtName.text = [NSString stringWithFormat:@"%@", placeHolderName];

现在在代码中,您不小心使用了逗号运算符,该运算符用于评估其第一个表达式(txtName .text = @%@"),舍弃结果并返回第二个(placeHolderName)

In code you have now you accidentally use Comma operator that evaluates its first expression (txtName.text = @"%@"), discards the result and returns the 2nd (placeHolderName)

这篇关于iPhone将NSUserDefaults输入到UITextField中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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