I am new to Swift and have been struggling with hiding the keyboard when the textField is on a custom tableview cell. I think the problem stems from the textField Reference being in the TableViewCell class, but i can't be sure. I have tried everything and am a little lost.
My code consists of:
TableViewCell:
import UIKit
class TableViewCell: UITableViewCell, UITextFieldDelegate
{
@IBOutlet weak var userText: UITextField!
@IBAction func answers(_ sender: UITextField)
{
}
override func setSelected(_ selected: Bool, animated: Bool)
{
super.setSelected(selected, animated: animated)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.delegate = self
textField.resignFirstResponder()
return true
}
}
and TableViewController:
import UIKit
class TableViewController: UITableViewController, UITextFieldDelegate
{
var textfield = TableViewCell()
override func viewDidLoad()
{
super.viewDidLoad()
let myText = textfield.userText
textField.resignFirstResponder()
myText?.delegate = self
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
self.view.endEditing(true)
return false
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return 3
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
return cell
}
}
I tried running the textFieldShouldReturn
function from both classes but I can't get it to work.