Friday, April 29, 2011

Is UITextView empty?

Hi,

To know if an UITextView is empty I used the following code:

if ( [ [textField text] isEqualToString:@""] )

But unfortunately it somehow fails to tell if the text field is empty.

What are the other ways?

When [textField text] is empty, it equals to nil.

Thank you.

From stackoverflow
  • Perhaps try [[textField text] length] == 0?

    Ilya : Thank you, this works for me.
    Chris Lundie : This works even if [textField text] is nil, since in Objective-C you always get 0 returned from a nil object.
  • [textField hasText]
    

    See reference.

    Ilya : What you are pointing me to is UITextView. And UITextField doesn't have this method. Unfortunately.
    Koraktor : I already wondered why you named your variable textField while referencing to UITextView in the question title.

0 comments:

Post a Comment