I have a UITextField and I create it like so:
firstnameField1 = [[UITextField alloc] initWithFrame:CGRectMake(85+320*4, 80, 150, 30)];
[firstnameField1 setBorderStyle:UITextBorderStyleRoundedRect];
[firstnameField1 setPlaceholder:@"Firstname"];
[firstnameField1 setDelegate:self];
[firstnameField1 setReturnKeyType:UIReturnKeyNext];
[scrollViewController addSubview:firstnameField1];
When the user taps the return key I want to check if the text field has anything typed into it, if its empty, the user hasn’t typed anything, I want to return and show a label telling the user that field is required to be filled before they can continue, just like you see all over the place.
I do the following to check:
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
//Check if the user has typed anything
if (textField.text == @"") {
//If not, show 'required' labels
[firstNameRequiredLbl1 setAlpha:1];
[surnameRequiredLbl1 setAlpha:1];
[firstNameRequiredLbl2 setAlpha:1];
[surnameRequiredLbl2 setAlpha:1];
return YES;
}
//Do all my other stuff, cut out for ease of reading, 100% doesn't affect this anyway
return YES;
}
I have set a breakpoint on that last method and it jumps right past that if statement whether I type into that textfield or not.
Any ideas? Thanks.
You’ll want to change
textField.text == @""to:
textField.text.length == 0ortextField.text isEqualToString:@""What the
==operator is actually doing in this case is checking if the strings are stored in the same area of memory rather than whether or not they contain the same characters.