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

Ok, so I have a function that allows my user to use the keyboard to navigate to the next field (from which I got the code from SO). It works great. My problem is that once my user gets to the final textbox where I have selected “GO” as the back button and I want to use that button as the perfect submit button. Since the flow is through the form, its presentably correct, but the functionality at the end doesn't exist. I found the answer here, but it looks like it calls the same functions as the "next field" code. Therefore, they do not work well together. So, this is what the following 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 following block is displayed below the 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 fine until the last textbox. But since the last field is "Go", I am trying to emulate what I would do with the @IBAction button for the button, but instead of the go button. Here's where I got the idea / code for Go to work:

Ios keyboard "Go" action

Any ideas on how to implement them? Or maybe just to work with the following function and implement a "keyboard" action similar to @IBaction? Maybe just a better way to implement both of these so that they match up with each other? Any help is greatly appreciated!

CHANGE!

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 will help a lot, I guess

+3


source to share


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;
}

      



For a cleaner look for handling tab order and form submission, read my answer here .

+8


source


First set return Key Go of your textbox then do the following method

func textFieldShouldReturn(textField: UITextField) -> Bool {
      if (textField.returnKeyType==UIReturnKeyType.Go)
         {
            //implemnt yor Ibaction method here
        }
      return true
 }

      



then see below imgae: enter image description here

+3


source







All Articles