按下输入和/或按键盘区域后,如何让键盘辞职?

时间:2016-04-02 22:47:38

标签: ios swift keyboard uitextfield

我在stackoverflow上看了几个其他的答案,但我似乎无法将它们应用到我的代码中:

import UIKit
class GraphItemCell: UITableViewCell, UITextFieldDelegate {

@IBOutlet weak var itemNameLabel: UITextField?
@IBOutlet weak var itemDescriptionLabel: UITextField?
@IBOutlet weak var pointScore: UITextField?


override func awakeFromNib() {
    super.awakeFromNib()
    // Initialization code
}

override func layoutSubviews() {
    super.layoutSubviews()
}
//TextField delegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
    textField.resignFirstResponder()
    return false
}


override func setSelected(selected: Bool, animated: Bool) {
    super.setSelected(selected, animated: animated)

    // Configure the view for the selected state
}


}

5 个答案:

答案 0 :(得分:0)

在UITableViewCell中为点击手势添加UIGestureRecognizer,并调用[itemNameLabel endEditing]&&轻触单元格时[itemDescriptionLabel endEditing]。

答案 1 :(得分:0)

//当用户触摸键盘外时取消键盘

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

这是我在我的应用中使用的

答案 2 :(得分:0)

在StoryBoard中设置TextField委托。

func textFieldShouldReturn(textField: UITextField) -> Bool {
            activeTextFiled=textField
            return activeTextFiled.resignFirstResponder();
        }

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

        }

答案 3 :(得分:0)

向您的UITableView添加手势识别器,然后在用户点按您的桌面视图时调用endEditing(true)方法

答案 4 :(得分:-1)

首先在textFieldShouldReturn中返回true,返回false表示您拒绝停止编辑。 第二个是在touchesbegin写的时候在外面按下时解雇键盘

self.view.endEditing(true)
相关问题