Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How could I enable / disable keyboard Return Key manually in Swift?

This question is not duplicated from these:

  • How to disable/enable the return key in a UITextField?

  • How to enable or disable the keyboard return key

  • Enable and Disable Keyboard return key on demand in iOS

I have two TextFields.

@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
  • textField1 has the Next button like the Return Key;

  • textField2 has the Go button like the Return Key;

textField1

textField2

I would like to enable the Go button of the second TextField just if both TextFields are not empty.

I tried to use someTextField.enablesReturnKeyAutomatically with TextFieldDelegate, but did not work.

Thank you for help.

like image 741
Calebe Emerick Avatar asked Feb 11 '16 21:02

Calebe Emerick


People also ask

How do I turn on the return button on my keyboard?

While on the ANSI keyboard, you can find the return key on the third row, above the right-hand Shift key and below the backslash \ key.

How do I enable the keyboard in Swift?

To enable SwiftKey and make it your default keyboard on stock Android, head to settings, select Language & input, and choose SwiftKey from the list of options.

How do I turn off keyboard Swift?

Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.


1 Answers

Below: textField2 is disabled as long as textField1 is empty. If the latter is non-empty, we enable textField2, but enable the Go button only if textField2 is non-empty (via .enablesReturnKeyAutomatically property),

/* ViewController.swift */
import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField2: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // text field delegates
        textField1.delegate = self
        textField2.delegate = self

        // set return key styles
        textField1.returnKeyType = UIReturnKeyType.Next
        textField2.returnKeyType = UIReturnKeyType.Go

        // only enable textField2 if textField1 is non-empty
        textField2.enabled = false

        // only enable 'go' key of textField2 if the field itself is non-empty
        textField2.enablesReturnKeyAutomatically = true
    }

    // UITextFieldDelegate
    func textFieldShouldReturn(textField: UITextField) -> Bool {

        if (textField1.text?.isEmpty ?? true) {
            textField2.enabled = false
            textField.resignFirstResponder()
        }
        else if textField == textField1 {
            textField2.enabled = true
            textField2.becomeFirstResponder()
        }
        else {
            textField.resignFirstResponder()
        }

        return true
    }
}

Runs as follows:

enter image description here

like image 79
dfrib Avatar answered Oct 16 '22 01:10

dfrib