ios - 单击键盘上的 "Done"可关闭键盘,但不会被关闭

标签 ios swift uitextfield uitextfielddelegate

我试图通过单击“完成”按钮关闭键盘,但它不会被关闭。这是我到目前为止所做的:

我还添加了协议(protocol) UITextFieldDelegate

override func viewDidLoad() {
    super.viewDidLoad()

    texts.layer.cornerRadius = 10
    texts.delegate = self
    texts.keyboardType = .numbersAndPunctuation
    texts.returnKeyType = .done
}

func textFieldShouldReturn(textField: UITextField!) -> Bool {   //delegate method
    var number = CGFloat((texts.text! as NSString).floatValue)

    slide.setValue(Float(number), animated: true)

    textField.resignFirstResponder()

    return true
}

我希望它关闭,以便输入的值可以存储在 number 变量中,应用程序可以继续。

最佳答案

.endEditing 设置为 true 应该可以解决问题

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

关于ios - 单击键盘上的 "Done"可关闭键盘,但不会被关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52267904/

相关文章:

iphone - iOS 6 在 UiNavigationController 中自动旋转

php date() 没有收到年、月、日 : NSDate to php

ios - 在12小时的UIDatePicker中设置24小时的时间

swift - 将十进制转换为小时 :minutes:seconds

arrays - 快速将数组保存到 Parse 并从中检索

ios - Cocoapods 私有(private) Pod 内的 Swift 框架

ios - NSCache 持有指向用 imageWithData : and does not remove from memory on unload 实例化的 UIImage 的强指针

ios - Swift iOS - 如何让文本字段在关闭和打开时根据预测文本移动

iOS-UITextField : extend the bottom line for ANY resolution

swift - 如何呈现带有文本字段的 UIAlertController,但该文本字段不能是第一响应者?