UITextField.text is not always there!

by Josh Highland on July 30, 2010

xcode UITextField.text is not always there!

If you have UITextField in your app and you want to validate a value or  see is there something in it you might think to write some code like this:

if (![addNewCellTextField.text isEqualToString:@""])

You would be right, but this will fail if the field is never touched! When a user taps the field, and it gets focus, UITextField.text = nil. That means that the condition above would be true, thats not what we want at all!

But once the UITextField is touched, the value becomes an instance of NSString with value of @””.  So make sure that you write your code to check for the nil case, like this:

if (addNewCellTextField.text != nil && ![addNewCellTextField.text isEqualToString:@""])
  • digg UITextField.text is not always there!
  • facebook UITextField.text is not always there!
  • stumbleupon UITextField.text is not always there!
  • twitter UITextField.text is not always there!
  • delicious UITextField.text is not always there!
  • reddit UITextField.text is not always there!
  • friendfeed UITextField.text is not always there!
  • posterous UITextField.text is not always there!
  • tumblr UITextField.text is not always there!

Leave a Comment

Previous post:

Next post: