i have a tableview with a custom cell and a textfield on it. Like image below.
Class of the Custom Cell:
import UIKit
protocol TextFieldTableDelegate{
func retornaTexto(sender: TextFieldTableViewCell, texto: String)
}
class TextFieldTableViewCell: UITableViewCell, UITextFieldDelegate {
@IBOutlet weak var textField: UITextField!
@IBOutlet weak var label: UILabel!
var delegate: TextFieldTableDelegate?
override func awakeFromNib() {
super.awakeFromNib()
self.textField.delegate = self
}
override func setSelected(selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
self.delegate!.retornaTexto(self, texto: textField.text!)
return true
}
}
This delegate method is never called
func retornaTexto(sender: TextFieldTableViewCell, texto: String) {
print(texto)
codigoAreaSelecionada = texto
}
The problem is, after i touch the textfield it opens a Number Pad, but when i touch outside it (on the background) it doesn't dismiss.
i've tried this Swift - How to dismiss number keyboard after tapping outside of the textfield but it broke all other touches things in the view.
EDIT: i make the Number Pad keyboard hide and show adding this to my viewController, but it not trigger the delegate method.
var tapRecognizer : UITapGestureRecognizer?
func keyboardWillHide(notification: NSNotification) {
view.removeGestureRecognizer(tapRecognizer!)
}
func keyboardWillShow(notification: NSNotification) {
tapRecognizer = UITapGestureRecognizer(target: self, action: "didTapView")
view.addGestureRecognizer(tapRecognizer!)
}
func didTapView(){
self.view.endEditing(true)
}
EDIT:
i've changed the method:
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
self.delegate!.retornaTexto(self, texto: textField.text!)
return true
}
with: func textFieldDidEndEditing(textField: UITextField) {
if let texto = textField.text{
self.delegate!.retornaTexto(self, texto: texto)
}
}
but it stops without a console warning on the call of retornaTexto.