2

I have a multiview process the user is entering in data and I'm saving it to the model class properties each step along the way.

I use textFieldDidEndEditing to check if the input is valid, and if so, saves the entered data.

On the view I have a continueButtonClicked event that checks to see if all the validations pass and if so loads the next view. I do NOT set the properties of the model here, because I think I shouldn't have to since each field is saved to the model 1 field at a time. However, I noticed some issues.

If the user is inside of a textbox and clicks the "Continue" button, the continueButtonClicked event fires BEFORE the textFieldDidEndEditing. What ends up happening is that the next view is populated with the "old" model prior to the save happening in textFieldDidEndEditing.

What am I missing? Is it proper to set all the properties on the Continue? That's how I would do it if I were programming for the web, but it doesn't seem right for a native app.

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
jaySF
  • 94
  • 1
  • 10

2 Answers2

4

You can call resignFirstResponder on the active textfield/textview (or on all of them), which will trigger the textFieldDidEndEditing.

In general, for such text editing issues, another approach is to abuse the -textField:shouldChangeCharactersInRange:... delegate method, in which you can determine the new value for every character entered. You should only revert to this method if there is no other way.

mvds
  • 45,755
  • 8
  • 102
  • 111
  • Thank you. I will look into this method as well when I get some time - just to wrap my head around it. mbh's solution above just happened to be quicker for me to try, but I'm curious about this one as well – jaySF Feb 17 '12 at 03:19
1

Can you try this instead and see if that solves your problem?

textFieldShouldEndEditing:
mbh
  • 3,302
  • 2
  • 22
  • 24
  • Awesome! This seems to have solved the issue. I'm a little confused as to why textFieldShouldEndEditing fires twice though: once before the continueButtonClick for ALL fields, and a second time for active field after the continueButtonClick event fires. It seems harmless, but just curious. – jaySF Feb 17 '12 at 03:17
  • If it is the active one you really want, you can always determine that by testing if thats the first responder. – mbh Feb 17 '12 at 04:29