Uitextfield - Capture Return Button Event

UITextField - capture return button event

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return NO;
}

Don't forget to set the delegate in storyboard...

Sample Image

I want an event to trigger with the enter/return button and my UITextField

Why can't you use

    - (BOOL)textFieldShouldReturn:(UITextField *)textField {

// You can trigger your events here.

return NO;
}

and also set delegate in storyboard. Whenever the user clicks on enter/return, this method will get called. You can trigger your event accordingly.

entering text into UI textfield by pressing return button

This should be as simple as using the UITextFieldDelegate to process when the return key has been pressed.

So you can adopt that protocol, assign the delegate on your UITextField and then process data in the delegate method textFieldDidEndEditing: when the return key is pressed. The delegate method I mentioned can be found here. Make sure you have enabled the processing of the return key by returning true from textFieldShouldReturn:.

how to add an action on UITextField return key?

Ensure "self" subscribes to UITextFieldDelegate and initialise inputText with:

self.inputText.delegate = self;

Add the following method to "self":

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == self.inputText) {
[textField resignFirstResponder];
return NO;
}
return YES;
}

Or in Swift:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == inputText {
textField.resignFirstResponder()
return false
}
return true
}

How to set action to return key in ios

it's simple

  1. youPasswordtextField.delegate = self ; // in viewDidLoad or any suitable place

  2. in your controllers .h file conform to UITextFieldDelegate protocol

3.implement delegate method

- (BOOL)textFieldShouldReturn:(UITextField *)textField // this method get called when you tap "Go"
{
[self loginMethod];
return YES;
}

-(void) loginMethod
{
// implement login functionality and navigate user to next screen
}

Using Next as a Return Key

Make sure your text fields have their delegate set and implement the textFieldShouldReturn method. This is the method that is called when the user taps the return key (no matter what it looks like).

The method might look something like this:

func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == self.field1 {
self.field2.becomeFirstResponder()
}

return true
}

The actual logic in here might vary. There are numerous approaches, and I'd definitely advise against a massive if/else chain if you have lots of text fields, but the gist here is to determine what view is currently active in order to determine what view should become active. Once you've determined which view should become active, call that view's becomeFirstResponder method.


For some code cleanliness, you might consider a UITextField extension that looks something like this:

private var kAssociationKeyNextField: UInt8 = 0

extension UITextField {
var nextField: UITextField? {
get {
return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
}
set(newField) {
objc_setAssociatedObject(self, &kAssociationKeyNextField, newField, .OBJC_ASSOCIATION_RETAIN)
}
}
}

And then change our textFieldShouldReturn method to look like this:

func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.nextField?.becomeFirstResponder()
return true
}

Once you've done this, it should simply be a matter of setting each text field's new nextField property in viewDidLoad:

self.field1.nextField = self.field2
self.field2.nextField = self.field3
self.field3.nextField = self.field4
self.field4.nextField = self.field1

Although if we really wanted, we could prefix the property with @IBOutlet, and that would allow us to hook up our "nextField" property right in interface builder.

Change the extension to look like this:

private var kAssociationKeyNextField: UInt8 = 0

extension UITextField {
@IBOutlet var nextField: UITextField? {
get {
return objc_getAssociatedObject(self, &kAssociationKeyNextField) as? UITextField
}
set(newField) {
objc_setAssociatedObject(self, &kAssociationKeyNextField, newField, .OBJC_ASSOCIATION_RETAIN)
}
}
}

And now hook up the nextField property in interface builder:

Sample Image

(Set up your delegate while you're here too.)


And of course, if the nextField property returns nil, the keyboard just hides.

Change 'Return' button function to 'Done' in swift in UITextView

You can set the return key type of the text field:

textField.returnKeyType = UIReturnKeyType.done

Update
You can definitely use the same approach to set the return key to "Done", as mentioned above. However, UITextView doesn't provide a callback when user hits the return key. As a workaround, you can try to handle the textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) delegate call, and dismiss the keyboard when you detect the input of a new line character:

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if (text == "\n") {
textView.resignFirstResponder()
}
return true
}

How do I get the return key to perform the same action as a button press in Swift?

Make sure your class extends the UITextFieldDelegate protocol

SomeViewControllerClass : UIViewController, UITextFieldDelegate

You can perform action as follows:

override func viewDidLoad() {
super.viewDidLoad()

self.textField.delegate = self
}

func textFieldShouldReturn(textField: UITextField) -> Bool {

//textField code

textField.resignFirstResponder() //if desired
performAction()
return true
}

func performAction() {
//action events
}


Related Topics



Leave a reply



Submit