我的应用程序包含以下内容:
- 主要数据视图控件
- 设置视图控制器(有 3 个设置文本字段)
在 (2).m 中,当按键被按下时,使用以下代码:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if(textField==_serverAddress){
NSLog(@"new server adress %@", textField.text);
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"serverAddress"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if(textField==_login){
NSLog(@"new login %@", textField.text );
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"login"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
if(textField==_password)
{
NSLog(@"new password %@", textField.text);
[[NSUserDefaults standardUserDefaults] setObject:textField.text forKey:@"password"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[textField resignFirstResponder];
return YES;
}
在 viewDidLoad 中,使用了以下代码:
- (void)viewDidLoad
{
// Get the stored data before the view loads
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *savedServerAddress = [defaults objectForKey:@"savedServerAddress"];
NSString *savedLogin = [defaults objectForKey:@"savedLogin"];
NSString *savedPassword = [defaults objectForKey:@"savedPassword"];
// Update the UI elements with the saved data
self.serverAddress.text = savedServerAddress;
self.login.text = savedLogin;
self.password.text = savedPassword;
[super viewDidLoad];
_serverAddress.delegate = self;
_login.delegate = self;
_password.delegate = self;
}
当我在 (2) 中将设置更改为新设置然后返回 (1) 然后返回 (2) 我看到纯文本字段时,我做错了什么?