6

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

Community
  • 1
  • 1
Sin
  • 265
  • 2
  • 7
  • 20

3 Answers3

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

Community
  • 1
  • 1
Christian Schnorr
  • 10,768
  • 8
  • 48
  • 83
  • 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 – Sin Jul 01 '15 at 10:02
  • @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. – Christian Schnorr Jul 01 '15 at 10:18
  • 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 Jul 01 '15 at 10:25
  • @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. – Christian Schnorr Jul 01 '15 at 10:56
7

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

Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71
kirti Chavda
  • 3,029
  • 2
  • 17
  • 29
  • 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? – Sin Jul 01 '15 at 10:51
  • first set break point and check textFieldShouldReturn methofd is called or not ? – kirti Chavda Jul 01 '15 at 10:58
  • @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. – Christian Schnorr Jul 01 '15 at 12:46
  • 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 – Sin Jul 01 '15 at 13:55
0

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)
    }
}
gandhi Mena
  • 2,115
  • 1
  • 19
  • 20