-1

I have a UITableViewCell and in it there is a reference to a UITextField:

@IBOutlet weak var inputTextSelectAnswer: UITextField!

i'm trying to add a "Done" button ontop of the keyboard and while u tap it the keyboard will be closed >

func setDoneOnKeyboard() {
        let keyboardToolbar = UIToolbar()
        keyboardToolbar.sizeToFit()
        let flexBarButton = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let doneBarButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissKeyboard))
        keyboardToolbar.items = [flexBarButton, doneBarButton]

        self.inputTextSelectAnswer.inputAccessoryView = keyboardToolbar
    }

since i don't call the UITextField in my controller itself, but in my UITableViewCell is there a way to dismiss the keyboard it throws an error

Use of unresolved identifier 'view'

here is the code:

//CANNOT DO THE FUNCTION BELOW
    @objc override func dismissKeyboard() {
        view.endEditing(true)
    }

how can this be done from a UITableViewCell ?

Chief Madog
  • 1,738
  • 4
  • 28
  • 55

2 Answers2

1

You can add that

weak var delegate:YourVC?

then inside cellForRowAt

cell.delegate = self

then

delegate?.view.endEditing(true)

Also you can use this directly inside the cell

self.endEditing(true)

The UITableViewCell subclass doesn't contain a view property it's inside a UIViewController subclass

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0

If you are adding the tool bar for a specific text field, calling resignFirstResponder() would be more appropriate:

Notifies this object that it has been asked to relinquish its status as first responder in its window.

Keep in mind that it is related to the window of the text field, not a particular view, i.e you don't have to care about the view for end its editing in your case.

Call

inputTextSelectAnswer.resignFirstResponder()

when you are aiming to hide the keyboard the inputTextSelectAnswer text field.


Furthermore:

resignFirstResponder vs. endEditing for Keyboard Dismissal

Ahmad F
  • 30,560
  • 17
  • 97
  • 143