iosxcodeuitextfielduicontrolevents

What does the term 'session' mean in layman terms for the editingDidBegin property of the UIControlEvent object?


While following a tutorial, I prejudged the next step of the tutorial and decided to try it on my own. The objective is to connect an IBAction function to a touch event so that once we begin editing a UITextField then it will update a UILabel. I jumped ahead of the tutorial that suggests connecting to the 'Editing Changed' option in the connections inspector, but I unknowingly made a connection to the 'Editing Did Begin' because it made sense verbally.

I ran the application and my UILabel wasn't updating. Therefore I re-read the tutorial slowly and noticed that I needed to use the 'Editing Changed' touch event instead. I wanted to understand why it didn't work initially so after checking Apple's reference material on UIControlEvents, I noticed that it uses the terms 'editing session'. Can anyone explain what a session is?

Hopefully this way I'll understand why my process didn't work on the first try.

enter image description here

class ConversionViewController: UIViewController {
    @IBOutlet var celsiusLabel: UILabel!
    @IBAction func fahrenheitFieldEditingChanged(textField: UITextField){
        celsiusLabel.text = textField.text
    }
}

Thank you in advance


Solution

  • The "editing session" is just the time from when the user begins interacting with the text field (the text field gets the focus) to the time the user finishes interacting with the text field (the text field loses focus).

    To put it another way, when a user taps on a text field, the keyboard is shown (usually), the "Editing Did Begin" event is sent, and the session begins. As the text is changed (via typing, cutting, or pasting), each change causes a "Editing Changed" event. When the focus leaves the text field, the keyboard is dismissed, the "Editing Did End" event is sent, and the session ends.

    Note that an "editing session" is just a high level concept.

    Your code didn't work when connected to the "Editing Did Begin" event because the text field isn't changing when that event is sent. It's an event simply telling you that the text field now has focus. That's it.