How to select a UITextField with IBAction to make it FirstResponder?

56 Views Asked by At

I have implemented multiple TextFields in a static TableViewController like this:

import UIKit

class GameViewController: UITableViewController, UITextFieldDelegate {
    @IBOutlet weak var inputField11:UITextField?
    @IBOutlet weak var inputField12:UITextField?
    ...
    override func viewDidLoad() {
        super.viewDidLoad()
        inputField11!.delegate = self
        inputField12!.delegate = self
        ...
        inputField11?.becomeFirstResponder()
    }
}

I can't seem to find a way to implement a way to change the active TextField with an IBAction. I have an inputFieldDidChange IBAction method implemented that works fine, triggered by "Editing Changed" in the storyboard:

@IBAction func inputFieldDidChange(inputField: UITextField?) {
    switch inputField {
        case inputField11:
            inputField12?.becomeFirstResponder()
        case inputField12:
            inputField13?.becomeFirstResponder()
        ...
}

But for some reason the same implementation method doesn't work for my inputFieldSelected method, triggered by "Touch Down":

@IBAction func inputFieldSelected(inputField: UITextField?) {
    switch inputField {
        case inputField11:
            inputField11?.becomeFirstResponder()
        case inputField12:
            inputField12?.becomeFirstResponder()
        ...
    }
}

I also tried doing this with "Touch Up Inside", "Editing Did Begin" and tap gesture recognizers.

0

There are 0 best solutions below