UITextField - return return event

How can I determine when the user clicked the back button while editing a UITextField? I need to do this to remove the keyboard when the user clicks the back button.

Thank.

+93
ios iphone cocoa-touch uitextfield backspace
Jun 10 '09 at 16:52
source share
6 answers
- (BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return NO; } 



Remember to specify the delegate in the storyboard ...

enter image description here

+207
Jun 10 '09 at 17:05
source share

No delegation is required, here is one-line:

 - (void)viewDidLoad { [textField addTarget:textField action:@selector(resignFirstResponder) forControlEvents:UIControlEventEditingDidEndOnExit]; } 

Unfortunately, you cannot directly do this in your storyboard (you cannot connect actions to the control that emits them on the Storyboard), but you can do this through an intermediary action.

+45
Jan 08
source share

SWIFT 3.0

 override open func viewDidLoad() { super.viewDidLoad() textField.addTarget(self, action: #selector(enterPressed), for: .editingDidEndOnExit) } 

the enterPressed () function contains all your actions after

 func enterPressed(){ //do something with typed text if needed textField.resignFirstResponder() } 
+5
Aug 21 '17 at 14:30
source share

Now you can do this in the storyboard using the sent event "Made an end on exit."

In your opinion, the controller subclass:

  @IBAction func textFieldDidEndOnExit(textField: UITextField) { textField.resignFirstResponder() } 

In your storyboard for the desired text field:

enter image description here

+4
Feb 27 '18 at 23:18
source share

Swift version using UITextFieldDelegate:

 func textFieldShouldReturn(_ textField: UITextField) -> Bool { resignFirstResponder() return false } 
+1
Sep 21 '18 at 21:29
source share
 - (BOOL)textFieldShouldReturn:(UITextField *)txtField { [txtField resignFirstResponder]; return NO; } 

When you press the enter button, this delegate method is called. You can grab the return button from this delegate method.

-one
Apr 30 '16 at 10:07
source share



All Articles