简体   繁体   中英

Hiding the Keyboard triggered by textField via resignFirstResponder

Environment: Xcode Version 8.2 (8C38)/Swift 3.0

A textFiled object in the View is wire up to a method named textFieldReturn in the controller via IBAction. The related codes are presented as follow

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
    }

@IBAction func textFieldReturn(_ sender: Any) {
        _ = (sender as AnyObject).resignFirstResponder()
    }

}

What I expect :

When I hit the Return key of the virtual keyboard the function textFieldReturn(_:) will be called and the keyboard will be hidden

Issue Observer :

The function is not called after I tapped the return key, the keyboard is still there

Resource :

This code spinet come from the example of the Chapter 16 of the book iOS 10 App Development Essentials by Neil Symth (pp-114)

The only difference between this code and original code is the type of the function argument (Sender). It is AnyObject in the original book while I've got Any by default, therefore I've cast to AnyObject inside the function body

Question :

Its seems to be a decent book, but the sample code doesn't work for me. How can I call the resignFirstResponder() method when I hit the return key


Alternative try out :

Instead of using IBAction , I turn to the idea of delegate, I've set the VeiwController as the delegate of the textField

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var tempText: UITextField! //reference the TextField as the variable **tempText** in the controller  

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tempText.delegate = self //set up the delegation 
    }

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

Problem

The alternative solution still not working.

Thanks for your time and help

Replace your textFieldReturn method with this and it should work just fine.

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

Make sure to keep the following in your viewDidLoad()

self.tempText.delegate = self

Why are you not using the original delegate function of UITextField ?

I think the default function will work as you want:

class ViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet weak var tempText: UITextField! //reference the TextField as the variable **tempText** in the controller  

    override func viewDidLoad() {
        super.viewDidLoad()
        self.tempText.delegate = self //set up the delegation 
    }

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

        return false
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM