Search code examples
swiftxcodeuitableviewkeyboard

Can't dismiss KeyBoard when a Done button pressed in UITableViewCell


First, done button codes are below

class ViewController: UIViewController, UITextFieldDelegate {
    let inputNumber = UITextField(frame: CGRect(x: 150.0, y: 100.0, width: 200.0, height: 50.0))
    let toolBarKeyBoard = UIToolbar()
    let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
    let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
    var result : String!

override func viewDidLoad() {
    super.viewDidLoad()


    calculatePrice()

}


func calculatePrice () {

    priceInputLabel.keyboardType = .numberPad
    priceInputLabel.clearButtonMode = .whileEditing

    self.view.addSubview(priceInputLabel)

    toolBarKeyBoard.sizeToFit()

    toolBarKeyBoard.setItems([flexibleSpace, doneButton], animated: false)

    priceInputLabel.inputAccessoryView = toolBarKeyBoard

}

@objc func donePressed() {
        view.endEditing(true)

    }
}

It works OK. When I touch 'inputNumber(UITextField)', a keyboard pops up. And when I input Number and touch 'Done' button, a keyboard dismisses. Good.

But, in other codes, down below, doesn't work.

class FruitTableViewCell: UITableViewCell, UITextFieldDelegate {

var fruitsTextField = UITextField()
let toolBarKeyBoard = UIToolbar()
let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: nil, action: #selector(donePressed))
var result : String!


override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)

    self.contentView.addSubview(fruitsTextField)

}

override func layoutSubviews() {
    super.layoutSubviews()
    fruitsTextField.frame = CGRect(x: 250, y: 7.5, width: 100, height: 30)
    fruitsTextField.textColor = UIColor(red: CGFloat(242/255.0), green: CGFloat(56/255.0), blue: CGFloat(90/255.0), alpha: 1.0)
    fruitsTextField.keyboardType = .numberPad
    fruitsTextField.clearButtonMode = .whileEditing

    toolBarKeyBoard.sizeToFit()

    fruitsTextField.inputAccessoryView = toolBarKeyBoard

    toolBarKeyBoard.setItems([flexibleSpace, doneButton], animated: false)


}


@objc func donePressed() {
    fruitTextField.endEditing(true)
    }

I can build, I can toggle a keyboard, I can touch a done button, but it doesn't dismiss a keyboard. I think, the function '@objc func donePressed()' at the bottom line is matter.

First codes are 'view.endEditing(true)' but these are 'fruitTextField.endEditing(true)'

So, I tried to change codes.

@objc func donePressed() {
    contentView.endEditing(true)
    }

But doesn't work.

Question1. How can I dismiss a keyboard?

Question2. Why doesn't a keyboard dismiss even though I touched 'Done' button?

Question3. In second code, is a keyboard not the FirstResponder?

Question4. In second code, what is the View for '.endEditing'?

Thanks!


Solution

  • Change your "done button" initialization to:

    lazy var doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(donePressed))
    

    You need target: self, and you need it to be lazy in order for self to be valid when the button is instantiated.

    You can also change your done func to:

    @objc func donePressed() {
        fruitsTextField.resignFirstResponder()
    }
    

    Doesn't really change the functionality, but I believe it is the recommended method.