Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the textFieldShouldReturn function not being called?

Tags:

swift

keyboard

The textFieldShouldReturn function is not being called at all: there are no errors but the keyboard does not respond at all.

My case is different from How to hide keyboard in swift on pressing return key? as in my case nothing is happening at all and other cases are in Objective-C.

Here is my code:

import UIKit

class ViewController: UIViewController {

    @IBOutlet var textField: UITextField!

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        resignFirstResponder()
        return true
    }
}

textField is an outlet to a text field on my storyboard. I also tried self.endEditing instead of resignFirstResponder.

like image 660
GJZ Avatar asked May 22 '15 21:05

GJZ


5 Answers

The rest of this answer is still very useful, and I'll leave it there as it can potentially help other askers... but here, I missed the obvious problem with this specific example...

We're not calling resignFirstResponder on the text field. We're calling it on the view controller. We need to call it on the text field, so modify your code to look like this:

func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

A UITextField will only call the textFieldShouldReturn property on the object which is its delegate.

We can fix this programmatically by adding a viewDidLoad method to set that:

override func viewDidLoad() {
    super.viewDidLoad()
    self.textField.delegate = self
}

But we can also set this up via the storyboard at build time.

Right click on the textfield to check and see whether or not the delegate has been set:

enter image description here

If that circle next to delegate is unfilled, we haven't set the delegate for our UITextField yet.

To set the delegate, hover over this circle. It will change to a plus sign. Now click and drag to the view controller that you want to delegate the text field (the view controller the text field is part of).

enter image description here

When you've appropriately hooked the view controller up as a delegate, this menu should look like this:

enter image description here

like image 140
nhgrif Avatar answered Oct 16 '22 23:10

nhgrif


If using Swift 3+, you have to add an underscore before the first property. Like:

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return true
}

This is also very well documented in the Apple Documentation. https://developer.apple.com/documentation/uikit/uitextfielddelegate/1619603-textfieldshouldreturn

like image 23
Jordi Kroon Avatar answered Oct 17 '22 00:10

Jordi Kroon


I'm enrolled in a Swift 4 Udemy course, and the instructor said to add the UITextFieldDelegate class for the ViewController in addition to Cntrl - dragging from the textField to the ViewController button and selecting delegate.

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        textField.resignFirstResponder()
        return true
    }
}
like image 3
MadAsAWriter Avatar answered Oct 16 '22 23:10

MadAsAWriter


Well, in my case. I accidentally enable hardware keyboard. Make sure you unchecked "Connect to hardware keyboard" in order for the keyboard to show up in the simulator.

Hardware -> Keyboard -> Connect to hardware keyboard

Hope this will help others too!

like image 2
Zubli Quzaini Avatar answered Oct 17 '22 00:10

Zubli Quzaini


You can set all textFields delegate in a loop:

var tF: [UITextField] = []

tf = [my1TextField, my2TextField, my3TextField]

for textField in tf {
    textField.delegate = self
}
like image 1
Ing. Ron Avatar answered Oct 16 '22 23:10

Ing. Ron