我有一個具有用戶名和密碼字段的應用程序。我想在允許用戶停止編輯字段之前驗證輸入。爲此,我使用textFieldShouldEndEditing委託方法。如果輸入未驗證,我顯示一個UIAlertView。有條件地解除鍵盤問題
這種方法的工作原理如下:如果輸入未驗證,用戶不能離開字段。
要使鍵盤上的完成按鈕關閉鍵盤,我在文本框上調用了resignFirstResponder。
我遇到的問題是警報被調用兩次。如何讓警報不再顯示兩次?
編輯澄清
正在發生的事情是,出現警報,然後出現另一個警報。然後我必須解僱這兩個窗口來修復輸入。
這裏是textFieldShouldEndEditing方法
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"function called %@",textField);
if([textField.text length] == 0)
{
return YES;
}
if(textField == userName)
{
if([self userNameValidated:textField.text])
{
NSLog(@"name validated");
NSString *tempDonerName = [[NSString alloc] initWithString:(@"%@",userName.text)];
//[[NSUserDefaults standardUserDefaults] setObject:tempDonerName forKey:@"name"];
[tempDonerName release];
return YES;
} else {
NSLog(@"name did not validate");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Invalid Username",@"Invalid Username title")
message:NSLocalizedString(@"Please make sure there are no apostrophes,spaces in the username, and that the username is less than 12 characters",@"Invalid username message")
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK",@"OK Text")
otherButtonTitles:nil];
[alert show];
[alert release];
return NO;
}
} else if (textField == userPin) {
if([self userPinValidated:textField.text])
{
NSLog(@"pin validated");
//NSString *tempDonerPin = [[NSString alloc] initWithString:(@"%@",userPin.text)];
//[[NSUserDefaults standardUserDefaults] setObject:tempDonerPin forKey:@"pin"];
//[tempDonerPin release];
return YES;
} else {
NSLog(@"pin did not validate");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Invalid Password",@"Invalid Pin title")
message:NSLocalizedString(@"Please make sure there are no apostrophes in the password",@"Invalid password message")
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK",@"OK Text")
otherButtonTitles:nil];
[alert show];
[alert release];
return NO;
}
}else {
NSLog(@"code validate - shouldn't get called");
return YES;
}
}
你可以發佈你的代碼嗎? – jessecurry 2010-03-30 16:30:10
您是否意味着用戶在完成按鈕一次後會顯示兩次警報?或者用戶必須在完成名稱驗證之前按兩次(或三次?)完成按鈕?看起來你正在處理字符串有點奇怪,所以它可能與此有關。或者也許問題在於你的驗證方法。很難從這個角度來看。 – Felixyz 2010-03-30 19:53:00
這不會幫助你解決你的問題,但是你可以通過使用UITextField的「標籤」字段來指定哪個字段是哪個字段,然後使用一個字段來使你的代碼更加模塊化 - 並且可讀/自動記錄爲每個可能的標籤開關語句。 – Amagrammer 2010-03-30 23:23:08