Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 260
Sin Avatar asked Jul 01 '15 09:07

Sin


2 Answers

- (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.

like image 135
Christian Schnorr Avatar answered Oct 19 '22 01:10

Christian Schnorr


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

like image 27
kirti Chavda Avatar answered Oct 19 '22 00:10

kirti Chavda