3

I have a problem with the event sequence of the UITextFieldDelegate.

I have two UITableView A and B which are hosted by two UIViewController A and B which are themselves hosted by a container view each as follows (cells omitted from the drawing):

Layout

Tableview B has textfields (UITextField) in its cells with the following delegate functions implemented.

func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason)
{
    if reason == .committed
    {
        ...save content of textfield...
    }
}

func textFieldShouldReturn(_ textField: UITextField) -> Bool
{
    textField.resignFirstResponder()
    ...save content of textfield...
    return true
}

The user can, after entering text in the field either hit the return key or tap somewhere outside to save the data. This works as long as the user taps somewhere within viewcontroller B.

If the user taps somewhere within tableview A, the tableview's

tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

function is called first and only after that, the textfield delegates are called.

As the tableView(_:didSelectRowAt:) loads new data into tableview B this is obviously a problem.

I tried textFieldShouldEndEditing as proposed here but it is still only called after didSelectRowAt

How can I receive an event that the user ended editing the field before any other events triggered?

Nilanshu Jaiswal
  • 1,583
  • 3
  • 23
  • 34
Michael Konz
  • 503
  • 1
  • 3
  • 20

1 Answers1

2

I am not sure if you'd be able to modify the default behavior it currently has. One way you could try is within didSelectRowAt(:) method from ViewController A:

  1. Notify the Container VC about the cell selection event

  2. Let the Container VC call secondChildVC.view.endEditing() method on View Controller B, and that should trigger those events on the Text Field before code execution returns from the row selection method (in the First VC)

ppalancica
  • 4,236
  • 4
  • 27
  • 42
  • 1
    This solution works like a charm ! I wasn't aware that endEditing would traverse down the view tree. Thank you so much ! – Michael Konz Dec 04 '18 at 19:12