How to use the keyboard "Go" to submit UITextFields like a UIButton in Swift 2

11,680

Solution 1

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.returnKeyType == UIReturnKeyNext) {
        // tab forward logic here
        return YES;
    }
    else if (textField.returnKeyType == UIReturnKeyGo) {
        // submit action here
        return YES;
    }

    return NO;
}

On a cleaner way to handle tab order and form submitting, read my answer here.

Solution 2

First set return key to Go of your TextField, then implement the following method:

func textFieldShouldReturn(textField: UITextField) -> Bool {
      if (textField.returnKeyType == UIReturnKeyType.Go)
         {
            // Implement your IBAction method here
        }
      return true
 }

then see in below image:

enter image description here

Share:
11,680

Related videos on Youtube

Sin
Author by

Sin

An aspiring coder, fluent in HTML, CSS. Learning the ways of PHP, jQuery and SQL. Now learning Swift!

Updated on October 09, 2022

Comments

  • Sin
    Sin over 1 year

    Ok so I have a function that allows my user to use the keyboard to go to the next field (Which I got the code from SO) It works perfect. My problem is, once my user gets to the final text field, in which, I've selected "GO" as the return button, and I want to use the go as the ideal submit button. As the flow through the form goes, its presentably right, but the functionality at the end isn't there. I found an answer here, but it looks like its calling the same functions as the "next field" code. So they don't work well together. So here's what the Next Field code looks like:

    override func viewDidLoad() {
        super.viewDidLoad()
        // Keyboard Next Field & Delegate
        enterEmailTextField.delegate = self
        enterPasswordTextField.delegate = self
        self.enterEmailTextField.nextField = self.enterPasswordTextField
        self.enterPasswordTextField.nextField = self.enterNameTextField
        // ...
    }
    

    And the next block is displayed below override (which i'm sure you know) func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }

    // Next Field
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        if let nextField = textField.nextField {
            nextField.becomeFirstResponder()
        }
    
        return true
    }
    

    Ok that works just fine down to the last text field, But since the last field is "Go" I'm trying to mimic what I would do with say an @IBAction for a button, but instead the go button. Here's where I got the idea/code for the Go to work:

    Action of the "Go" button of the ios keyboard

    Any Ideas on how to implement these? Or maybe just to work with the next function, and implement a "keyboard go" action similar to an @IBaction? Maybe just an all around better way to implement both of these so they coincide with each other? Any help is much appreciated!

    EDIT!

    I forgot the actual NextField.swift File which I'm sure is important (sorry)

    import Foundation
    import UIKit
    
    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, UInt(OBJC_ASSOCIATION_RETAIN))
            }
        }
    }
    

    this would help a lot I'm assuming

  • Sin
    Sin almost 9 years
    quick question: Where would I put this block? I'm sure there is a function I could use to use this method more than one time, but where would i put it? thank you for the quick response, I also read your answer you linked as well
  • Christian Schnorr
    Christian Schnorr almost 9 years
    @Sin You have already implemented the method in the code sample you posted. Personally I would put it in the view controller managing the form.
  • Sin
    Sin almost 9 years
    Just a little confused here lol. So do I replace: // Next Field func textFieldShouldReturn(textField: UITextField) -> Bool { if let nextField = textField.nextField { nextField.becomeFirstResponder() } return true } with - (BOOL)textFieldShouldReturn:(UITextField *)textField { if (textField.returnKeyType == UIReturnKeyNext) { // tab forward logic here return YES; } else if (textField.returnKeyType == UIReturnKeyGo) { // submit action here return YES; } return NO; }
  • Sin
    Sin almost 9 years
    I tried this and I got an error, which I know is my fault cause i'm not getting it. I placed your first code directly under my Next Field code, and I get the error: Invalid redeclaration of 'textFieldShouldReturn'. Also, my final field is already set to "go" instead of return. can you tell me what i'm doing wrong?
  • Christian Schnorr
    Christian Schnorr almost 9 years
    @Sin My code is an Objective-C implementation of -textFieldShouldReturn:, the method you implemented in your sample too. I leave it up to you to convert it to Swift.
  • kirti Chavda
    kirti Chavda almost 9 years
    first set break point and check textFieldShouldReturn methofd is called or not ?
  • Christian Schnorr
    Christian Schnorr almost 9 years
    @Sin The compiler is telling you all you need to know. Invalid redeclaration. You declared the method twice. You should replace your current implementation, not add a second method with the same signature.
  • Sin
    Sin almost 9 years
    Thank you @ChristianSchnorr I don't want to ask too many questions...I'm trying to figure it out on my own, I feel like trial and error at least shows your trying, while not relying on an answer at every corner, But this one is tricky to me. Only because I'm trying to get it to work with this other code. I get what your saying about the error, I'm not sure what to replace