1

I have an app that has a mix of text fields and text views, and people interact with them in various ways. Some switch from entry field to entry field, some type into the field and hit "done," etc.

Has anyone found a way they particularly like to deal with switching between fields/views, while updating internal data models based on the inputted data as it happens? Any favorite tricks?

Relatedly: if somebody switches from one field to another, what is the order of delegate calls (the should/did begin/end editing) for each field, and how does resignFirstResponder() play into it?

Thanks!

Taylor Nelms
  • 384
  • 2
  • 16
  • 1
    You can answer your own "relatedly" question by implementing all of the delegate methods and logging a message including the passed in text field/view. Then examine the output to see what order everything happens. Learn by doing and experimenting. You'll get a much better understanding than someone just telling you. – rmaddy May 07 '16 at 16:48

1 Answers1

0

You can set unique tags to each UITextFields and UITextViews to differentiate and store data accordingly for each UITextField or UITextView. Whenever someone interacts with an UITextField, the following delegate methods can be used

func textViewDidBeginEditing(textView: UITextView)
func textFieldDidBeginEditing(textView: UITextField)

The above two delegate functions are called whenever we start editing the UITextField or UITextView. These functions can be used to adjust your layout accordingly so that your UITextField or UITextView doesn't go behind the Keyboard. However, in case of multiple UITextFields and UITextViews in the view, it' better to use a third party library to do this task. I used this https://github.com/michaeltyson/TPKeyboardAvoiding in one of my projects

func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool 

Using the above two functions, you can track the changes made to the text inside the UITextField and UITextView

func textViewDidEndEditing(textView: UITextView)
func textFieldDidEndEditing(textField: UITextField)

The above two functions are called whenever editing ends in a particular UITextView or an UITextField. This can be used for storing data from UITextField or UItextView according to tag.

func textFieldShouldReturn(textField: UITextField) -> Bool 

This is called when return key is clicked. You can also use this to store the text typed in this textfield in an array according to tag etc.

KrishnaCA
  • 5,615
  • 1
  • 21
  • 31