Hello! There are several UIViewController `s with a lot of textField and pickerView. Is there a feature that tracks ANY changes to the class. That is, changing the text in any one textField or the lines in pickerView. I just imagine the size of the method for comparing all these values ​​through if for example ...

  • No such. What for? - VAndrJ
  • you have delegates for everything, you can still attach KVO. That should be enough for you to track any changes - Max Mikheyenko
  • I didn’t quite understand how to do this ... I want to attach an alertController to the barButton so that it would crash when the user did not save the data if he made any changes. - Igor Zexyy

1 answer 1

For such purposes there is a delegate. For example, UITextFiledDelegate .

This delegate provides you with many opportunities to track changes. For example, the func textFieldDidEndEditing(UITextField) method func textFieldDidEndEditing(UITextField) is called when the user has finished entering text. Method

 func textField(UITextField, shouldChangeCharactersIn: NSRange, replacementString: String) 

invoked when the user enters text (work in this method is completed before the entered text is displayed in the field). Well, and many other methods, you can study them by reading the documentation.

Example:

 func textFieldDidEndEditing(textField: UITextField) { if textField == emailTextField { // тут будет некое действие если вы только что закончили вводить текст в emailTextField. } } 

If you have many textfield'ов you can use switch. But this is another topic for reflection =)