Sin
Sin

Reputation: 265

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

Upvotes: 6

Views: 12859

Answers (3)

gandhi Mena
gandhi Mena

Reputation: 2293

Available for iOS 15 in SwiftUI. You can use .submitLabel(.go) to do it.

@available(iOS 15.0, *)
struct TextFieldSubmitView: View {
    @Binding var name: String

    var body: some View {
        VStack(alignment: .leading) {
            TextField("Type text", text: $name)
                .textFieldStyle(.roundedBorder)
                .padding()
            #if os(iOS)
                .submitLabel(.go)
            #endif
        }
    }
}

@available(iOS 15.0, *)
struct TextFieldSubmitView_Previews: PreviewProvider {
    static var previews: some View {
        TextFieldSubmitView(name: .constant("Name"))
            .previewLayout(.sizeThatFits)
    }
}

Upvotes: 0

kirti Chavda
kirti Chavda

Reputation: 3015

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

Upvotes: 7

Christian Schnorr
Christian Schnorr

Reputation: 10776

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

Upvotes: 9

Related Questions