Search code examples
iphoneobjective-cioscocoa-touchuitextfield

How to check if UITextField text is empty when done editing


I currently have a UITextField with default text and set to clear when editing begins. I'm trying to make it so that if the field is empty or nil when editing is done the text value will be the default text again. I'm having trouble detecting when either editing is done or the keyboard is dismissed. Could anyone tell me which detection method I would want to use and how to implement it? Thanks so much!


Edit


A placeholder can't be used in my case


Solution

  • I think you don't need to handle "default text" yourself. Check placeholder property of UITextField class.

    Update

    So placeholder is not good for your case. Did you try to implement UITextFieldDelegate methods for your UITextField and change the text in textViewDidEndEditing:?

    To do so, implement UITextFieldDelegate methods those look useful for your scenario in your view controller, and set delegate of your UITextField to the view controller (either by Interface Builder or programmatically). Set the default text in textViewDidEndEditing: method.

    You can refer to the "Getting the Entered Text and Setting Text" section of Text, Web, and Editing Programming Guide for iOS.