0

i have a tableview with a custom cell and a textfield on it. Like image below.

Custom Cell

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.

Community
  • 1
  • 1
marchiore
  • 582
  • 2
  • 6
  • 21

2 Answers2

1

textFieldShouldReturn gets called when a TextField that has a full keyboard has the Return key tapped on it. The numeric keypad doesn't have a return key, so this isn't going to work for your case.

Probably the correct approach is to add a gesture recognizer to a view outside of the textbox (maybe the viewcontroller's view?) to dismiss the keyboard (like the question you referenced); but do this on the keyboard show; and remove it on the keyboard hide. There are UINotification events you can hook into for these events: See How to detect when keyboard is shown and hidden

You can also use these events to change the table insets for the table so the number entry cell is visable above the keyboard on smaller phones as well!

Community
  • 1
  • 1
Fiid
  • 1,852
  • 11
  • 22
  • it works, but the func retornaTexto(sender: TextFieldTableViewCell, texto: String) is never executed, so i'm not able to take the value of the field on my delegate – marchiore Nov 04 '15 at 02:10
1

Maybe you can try another method, modify the keyboardDismissMode property of UITableView.

Here is the cleanest way to achieve this in iOS 7.0.

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

Or to dismiss when touch

tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive;

UPDATE

I think that you can implement this delegate method about the end of editing.

-(void)textFieldDidEndEditing:(UITextField *)textField  
{
    // receive the end of editing of UITextField.
}

Or, obtain the editing event by the following way.

// Add a "textFieldDidChange" notification method to the text field control.
[textField addTarget:self 
              action:@selector(textFieldDidChange:) 
    forControlEvents:UIControlEventEditingChanged];
AechoLiu
  • 17,522
  • 9
  • 100
  • 118
  • this way dismiss the keyboard, but how can i trigger the retornaTexto(sender: TextFieldTableViewCell, texto: String) method with this? – marchiore Nov 04 '15 at 01:41
  • i've changed the func textFieldShouldReturn(textField: UITextField) -> Bool { with -(void)textFieldDidEndEditing:(UITextField *)textField, but it stops woking on the delegate call line. – marchiore Nov 09 '15 at 05:16
  • What it means about `stop working` ? If it crashes, you can add break points about `All Exceptions` to catch the error. [Refer this page](http://blog.manbolo.com/2012/01/23/xcode-tips-1-break-on-exceptions). – AechoLiu Nov 09 '15 at 08:25