UITextField save text
I've an UITextField fo开发者_如何学Gor insert the name of the user. And in the right of the navigation bar I have a button called "Next" for switch to the next view.
I've this method:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
[theTextField resignFirstResponder];
[[NSUserDefaults standardDefaults] setObject:theTextField.text forKey:@"name_post"];
return YES;
}
As you can see I use NSUserDefaults for save my temporary text. So, when I click the "return" key on the field's keyboard the text is saved. The problem is when I click the next button at the top while the field's keyboard is still visible, because the above method is not called, and the text is not saved.
How can I fix my problem?
Write a method called "saveText" with the content of your textFieldShouldReturn: method, and call it both from the textFieldShouldReturn: method and the action of the button.
- (BOOL) NextButtonAction {
[textField resignFirstResponder];
[[NSUserDefaults standardDefaults] setObject:theTextField.text forKey:@"name_post"];
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
return [self NextButtonAction];
}
change the target of Next button to NextButtonAction, and call NextButtonAction from the delegate method textFieldShouldReturn and also make the textField member of your class so you can access it from both of the functions.
You can save the textfield text on the next button click .This way it will be independent where your keyboard is visible or not
- (void)viewDidDisappear:(BOOL)animated{
NSString *inputOne = textview.text;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:inputOne forKey:@"InputOne"];
}
This Worked for me incase the user did not exit the keyboard in the expected way
You can add an nstimer to the button that makes the button wait 1 second before going on.
Its a bad solution, but one second isn't much. the user will just think that the app is loading something HUGE. Just add a large image on the next page.
精彩评论