2010-06-25 109 views
5

我正在研究一個包含多個UITextField對象的視圖。我的視圖控制器用作UITextFieldDelegate,並且我實現了(BOOL)textFieldShouldEndEditing:(UITextField *)textField方法來保存和驗證正在顯示的記錄。textFieldShouldEndEditing被調用多次

如果用戶在編輯項目並點擊保存/驗證失敗後點擊「完成」按鈕,則會顯示UIAlertView,並且用戶保留在驗證失敗的UITextField上。

我的問題是這樣的 - 當用戶從UITextField,將節省/驗證失敗到另一個UITextField S的點擊,則(BOOL)textFieldShouldEndEditing:(UITextField *)textField方法被調用多次,和UIAlertView多次彈出。

爲什麼(BOOL)textFieldShouldEndEditing:(UITextField *)textField在用戶點擊鍵盤上的「完成」時調用一次,但當用戶點擊另一個時調用多次UITextField

這裏是我的代碼:

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField { 
    NSLog(@"textFieldShouldEndEditing called by textField with text=%@", textField.text); 

    currentItem.nameOrNumber = nameOrNumber.text; 

    // Try to save the managed object. 
    NSError *error = nil; 
    if (![[currentItem managedObjectContext] save:&error]) {   
     UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Uh Oh!",@"") 
                  message:[error localizedDescription] 
                  delegate:self 
                cancelButtonTitle:NSLocalizedString(@"OK",@"") 
                otherButtonTitles:nil]; 
     [errorAlert show]; 
     [errorAlert release]; 
     shouldEnd = NO; 
    } 

    return shouldEnd; 
} 

回答

3

我認爲你的問題來自於,當您正在編輯一個文本框文本框方法被調用,直接挖掘到另一個的順序。

如果我沒有記錯的話,應該是這樣的(你正在編輯在A和點按B)

  • textFieldShouldBeginEditing場B
  • textFieldShouldEndEditing的領域的
  • textFieldDidEndEditing現場一個
  • textFieldDidBeginEditing場B

所以當喲你在textFieldShouldEndEditing方法中,textfield B已經成爲第一響應者。所以當你讓UIAlertView出現時,B會失去焦點,因此也會調用textFieldShouldEndEditing

這對我來說也是一個問題,當我想在textField開始編輯時提出一個視圖。我找到的解決方案是創建一個布爾類變量,指示我是否正在從一個textField切換到另一個。 我將它設置爲textFieldShouldBeginEditing中的TRUEtextFieldDidBeginEditing中的FALSE。當您在textFieldShouldEndEditing是,如果它被設置爲TRUE這意味着用戶直接拍了拍另一個文本框。然後,你只需要找到正確的方法,使您的測試只有一次(也許shouldEndEditing應該返回false或東西)。

0

看起來適合我被稱爲2次,每次測試場。 爲什麼?只是想想...已經過去了我還,讓我頭疼

你不能做一些像這樣

- (BOOL)textFieldShouldEndEditing:(UITextField *)txtField{ 

if(i_dont_know){ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
                message:@"Message" 
                delegate:self 
              cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
    [alert show]; 
    [alert release]; 
    return false; 
} 

return true;} 

是那個UIAlertView中表演也試圖辭職文本字段 的編輯和調用這個函數「textFieldShouldEndEditing:」...

所以我解決這個問題的方法是在界面中添加一個名爲「shouldEndEditing」的成員變量,它是true por默認值。 而「textFieldShouldEndEditing:」可以是這樣的一些。

- (BOOL)textFieldShouldEndEditing:(UITextField *)txtField{ 

if(shouldEndEditing == false) 
{ 
    shouldEndEditing = true; 
    return false; 
} 

if(i_dont_know){ 
    shouldEndEditing = false; 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
                message:@"Message" 
                delegate:self 
              cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
    [alert show]; 
    [alert release]; 
    return false; 
} 

return true;} 

好運...

1

另一種選擇是讓UIAlertView假正確的驗證和延遲校正部向後面的時間。事情是這樣的:在每個TextView的

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    double delayInSeconds = 0.; 
    self.currentTextField.text = @"Something that won't trigger validation error"; 
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC); 
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){ 
     // do what you need here 
    }); 
} 
0

你不能添加不同的代碼,並檢查在textFieldShouldEndEditing標籤?或者我錯過了這個觀點?