Best way to save a UITextField test: textFieldShouldReturn or textFieldDidEndEditing

My goal is simply to save the text on a UITextField after clicking the mouse button on the keyboard. I could do it in extFieldShouldReturn or textFieldDidEndEditing: doesn't matter? or is there a better approach?

Thanks!!

+3
source share
1 answer

textFieldShouldReturn is called only if the user presses the return key. If the keyboard is rejected for any other reason, such as a user selecting a different field or switching views to another screen, this will not be the way textFieldDidEndEditing will be.

A better approach is to use textFieldShouldReturn to reject the responder (hide the keyboard) as follows:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { //hide the keyboard [textField resignFirstResponder]; //return NO or YES, it doesn't matter return YES; } 

When the keyboard closes, textFieldDidEndEditing is called. Then you can use textFieldDidEndEditing to do something with the text:

 - (BOOL)textFieldDidEndEditing:(UITextField *)textField { //do something with the text } 

But if you really want to perform the action only when the user explicitly presses the "go" or "send" or "search" (or something else) button on the keyboard, then you should put this handler in the textFieldShouldReturn method, like this:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { //hide the keyboard [textField resignFirstResponder]; //submit my form [self submitFormActionOrWhatever]; //return NO or YES, it doesn't matter return YES; } 
+17
source

Source: https://habr.com/ru/post/975179/


All Articles