Iphone UITextFeld比较失败

Iphone UITextFeld比较失败,iphone,objective-c,ipad,uitextfield,Iphone,Objective C,Ipad,Uitextfield,我有一个UITextField,我这样创建它: firstnameField1 = [[UITextField alloc] initWithFrame:CGRectMake(85+320*4, 80, 150, 30)]; [firstnameField1 setBorderStyle:UITextBorderStyleRoundedRect]; [firstnameField1 setPlaceholder:@"Firstname"]; [firstnameField

我有一个UITextField,我这样创建它:

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];
当用户点击返回键时,我想检查文本字段中是否键入了任何内容,如果为空,则表示用户没有键入任何内容,我想返回并显示一个标签,告诉用户在继续之前,需要填充该字段,就像您在各处看到的一样

我执行以下操作以检查:

-(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;
}
我在最后一个方法上设置了一个断点,不管我是否在文本字段中键入,它都会跳过if语句


有什么想法吗?谢谢。

您需要更改
textField.text=@“

至:
textField.text.length==0
textField.text IsequalString:@”“


在这种情况下,
=
=
操作符实际做的是检查字符串是否存储在相同的内存区域,而不是它们是否包含相同的字符。

您需要更改
textField.text=@“

至:
textField.text.length==0
textField.text IsequalString:@”“


在这种情况下,
==
运算符实际做的是检查字符串是否存储在相同的内存区域,而不是检查它们是否包含相同的字符。

您无法以这种方式比较
NSString
文本字符串。改用这个:

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

您不能以这种方式比较
NSString
文本字符串。改用这个:

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