按下完成后,TextField会换行,而不是关闭键盘Xcode 10

时间:2018-10-20 09:45:38

标签: ios swift xcode

我正在学习Xcode和Swift,所以我决定阅读Apple Developer网站上的教程。我已将我的文本字段设置为关闭 如本教程所述,键盘,而不是消除键盘 键盘并执行

完成的操作
func textFieldDidEndEditing(_ textField: UITextField)

文本字段仅换行,而键盘不关闭。

在键入并按完成之前:

Image of Iphone 7 Simulator before typing and pressing return

键入并按Enter键后:

Image of Iphone 7 Simulator after typing and pressing return

这是我的代码:

My code

3 个答案:

答案 0 :(得分:0)

尝试一下:

class MyViewController: UIViewController, UITextFieldDelegate {

    @IBOutlet var textField : UITextField

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        self.textField.delegate = self
    }

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        self.view.endEditing(true)
        return false
    }
}

答案 1 :(得分:0)

确保它是一个UITextField而不是UITextView

答案 2 :(得分:0)

尝试:

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

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        textFieldTXT.delegate = self
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }

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

}