2012-01-11 123 views
0

我有一個的UITextField,我創建它像這樣:UITextFeld比較失敗

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; 
} 

我已經設置上的最後一個方法斷點,它跳到右邊過去,如果語句是否我輸入到該文本框或沒有。

任何想法?謝謝。

回答

2

你想改變textField.text == @""

到:textField.text.length == 0textField.text isEqualToString:@""

什麼==運營商實際上是在做這種情況下,如果字符串存儲在被檢查內存的相同區域而不是它們是否包含相同的字符。

1

您無法比較NSString這樣的文本字符串。改爲:

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