0

I know that this question may sounds duplicate, but looking at the several answer, nothing is working for me yet.

Basically I have a Quizz App, a question is shown and the user needs to fill several UITextFields to answer the question (i.e. if the answer is VENICE, 6 UITextFields will be shown, 1 per letter).

It was able to detect one character in the UITextFields and once the user hits a key it will jump to the following UITextField. I use the tag of the UITextField and the method becomeFirstResponder.

The problem is that I will like to detect the backspace when a UITextField is empty so I will jump to the previous UITextField.

I have tried this solution from Jacob Caraballo (Detect backspace in empty UITextField) but I am not sure, how to use it with my existing UITextField.

For example, I have tried:

 // @IBOutlet weak var textField1: UITextField!
    @IBOutlet weak var textField1:MyTextField!

But calling the delegate

textField1.myDelegate = self

It crashes

I also notice, that using the Jacob's solution I won't be able to check which UITextField was used as the func textFieldDidDelete() doesn't not have an UITextField as parameter and I will need to check its tag i.e.

If textField.tag == 5 {
   textField4.becomeFirstResponder()
}

Any help on this please?

1 Answers1

0

If you use Jacob's solution, the deleteBackward method will be called with the current UITextField instance. So you can add a protocol to that UITextField class and pass it back to your view. Something like this:

protocol MyTextFieldDelegate: class {
   func backwardDetected(textField: MyTextField)
}

class MyTextField: UITextField {
   weak var myTextFieldDelegate: MyTextFieldDelegate?

   override func deleteBackward() {
     super.deleteBackward()

     self.myTextFieldDelegate?.backwardDetected(textField: self)
   }
}
Lirik
  • 3,167
  • 1
  • 30
  • 31
  • Thanks Lyric, but how I have to declare my UITextField and how I set the delegate? @IBOutlet weak var textField1:MyTextField! declaration for each UITextField is correct? and How I set the delegate for each UITextField? Sorry, I am a little bit lost with this – Jorge Fernández Jun 12 '19 at 21:15
  • each of your delegate will have to be of type `MyTextField`. and for each of them add this line: self.textField1.myTextFieldDelegate = self – Lirik Jun 12 '19 at 21:17
  • after that implement the func backwardDetected(textField: MyTextField) method in your controller/view – Lirik Jun 12 '19 at 21:18
  • Sorry Lyric, I have an error on the class. It says: Value of type 'UITextFieldDelegate' has no member 'backwardDetected' – Jorge Fernández Jun 12 '19 at 21:37
  • fixed my code above. Its should be : self.myTextFieldDelegate?.backwardDetected(textField: self). Sorry about that ;) – Lirik Jun 12 '19 at 21:44
  • Thanks Lyric,and thanks a lot a lot for your patient :-). Now, when setting the delegate I have an error when trying to run the application in the line `self.textField1.myTextFieldDelegate = self`, the app is builded that just when it starts it throws the error Thread `1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT)`. Any ideas why? I have created a new app from the scratch – Jorge Fernández Jun 13 '19 at 08:07
  • I think the problem is on the declaration of the IBOutlets, to create an IBOutlet I Control + Drag my UITextView, and on Type I change default one (UITextField) to MyTextField, probably this is the problem, but I don't know another way to create the IBOutlets – Jorge Fernández Jun 13 '19 at 09:05
  • I solve it changing in the Identity Inspector from UITextField to MyTextField. Thanks a lot Lirik!!!!!!!!!!!!!!!!!!!!!!!!!!!! – Jorge Fernández Jun 13 '19 at 09:16
  • Happy to help! @JorgeFernández – Lirik Jun 13 '19 at 13:45