I want to refer to the value of an empty text field because I want make an if function that if there is nothing in the UITextField the text of the UITextField will become "nothing".
There is a better solution, but here is the check using an if-statement
if ([textField.text length] == 0) {
// nothing
textField.text = @"Nothing";
// textField.backgroundColor = [UIColor greyColor]; // maybe play around with color
}
The better solution would be to use UITextfield's placeholder property, which does exactly as your question states: show a text if the UITextField is empty.
Without having more context, I would say that the best way to go would be to set the placeholder property. Here's the information on that property from Apple's documentation:
@property(nonatomic, copy) NSString *placeholder
The string that is displayed when there is no other text in the text field. This value is nil by default. The placeholder string is drawn using a 70% grey color.
However, to answer your question directly, you can set the UITextField to a specific string (@"Nothing"
in your case) when it's empty by using the following example:
if([myUITextField.text length] == 0) {
myUITextField.text = @"Nothing";
}
More information on the UITextField class is available here.
hey guys thanks a lot.. this was really helpful but i have one question... why the hell cant it simple work like if(textfield.text == nil) then textfield.text=@"NOTHING";
i mean what is so wrong with this isnt it logical..?