How to change the behavior of the Done button on the keyboard?

Okay, so my goal is that when the “Finish” button is pressed after entering something into a UITextField , it should trigger one of my actions, but not reject the keyboard.

Right now, I hooked up a UITextField with my action through the “Unlock on Exit” event, and every time I click the Finish button, when I finish typing, the keyboard leaves.

Let me know if you need further clarification.

+4
source share
4 answers

Try setting textField.delegate = self in the code, and also in accordance with the UITextFieldDelegate protocol. Then do this method:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { // Do something } 
+9
source

You must use the delegate method of UITextField . (Install the delegate from TextField to fix viewController in Interface Builder or in source)

Next, go to the textField view manager file .h file:

 @interface correctViewController(type yours) :UIViewController <UITextFieldDelegate> //now correctViewController "understands" our textfield. 

Go to the correctViewController.m file:

 //Create delegate method of textField -(BOOL)textFieldShouldReturn:(UITextField *)textField{ [textField resignFirstResponder]; //to hide keyboard return NO; } 

It looks like this.

0
source

Take a look at UITextFieldDelegate Link: http://developer.apple.com/library/ios/#documentation/uikit/reference/UITextFieldDelegate_Protocol/UITextFieldDelegate/UITextFieldDelegate.html

This method can be used to control the behavior of your keyboard:

 - (BOOL)textFieldShouldReturn:(UITextField *)theTextField { return NO; } 
0
source

In Swift, it will be:

 //MARK: UITextFieldDelegate func textFieldShouldReturn(textField: UITextField) -> Bool { textField.resignFirstResponder() return true } 
0
source

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


All Articles