Download: UsandoNSUserDefaults.zip
// ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
if ([userDefault stringForKey:@"palavra"] == NULL) {
textField.text = @"";
}else{
textField.text = [userDefault stringForKey:@"palavra"];
}
[self btnOkTouched:nil];
}
- (IBAction)btnOkTouched:(id)sender
{
labelPalavra.text = [NSString stringWithFormat:@"Palavra: %@",textField.text];
int qtde = [textField.text length];
labelQtde.text = [NSString stringWithFormat:@"Quantidade: %d",qtde];
[textField resignFirstResponder]; //Tirar o teclado
}
- (IBAction)btnSalvarTouched:(id)sender
{
NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
[userDefault setObject:textField.text forKey:@"palavra"];
[userDefault setInteger:[textField.text length] forKey:@"quantidade"];
[userDefault synchronize]; //Forçar salvar os registros
}