2010-12-05 73 views
0

在我看來我有幾個UITextFields,我需要顯示一些鍵文字輸入和其他時間UIDatePicker。幾個UITextField,需要切換鍵盤和UIDatePicker

我爲了顯示一個DatePicker我爲日期UITextField實現了委託,但是一旦我爲文本輸入選擇另一個UITextField,DatePicker仍然保留在後臺。然後我試圖實現委託也爲其他的UITextField,使用此代碼:

- (void)viewDidLoad { 
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"dd/MM/yyyy"]; 
NSString *stringFromDate = [formatter stringFromDate:[NSDate date]]; 
NSLog(@"IN - AddExpenseViewController::viewDidLoad %s",stringFromDate); 
date.text = stringFromDate; 

datePicker.hidden = YES; 
date.delegate = self; 

amount.delegate = self; 

isIncome = NO; 


[super viewDidLoad]; } 

// ...

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ 
NSLog(@"IN - AddExpenseViewController::textFieldShouldBeginEditing %@",textField.placeholder); 
if (![textField.placeholder isEqual:[NSString stringWithFormat:@"Date"]]) { 
    [self hideDatePicker]; 
    [textField becomeFirstResponder]; 
    return YES; 
} 
else { 
    [self showDatePicker]; 
} 
return NO;} 

的問題是,當我選擇日期TextField和比量,委託被調用的循環:

2010-12-05 13:15:17.324 AddExpense [1179:207] IN - AddExpenseViewController :: textFieldShouldBeginEditing金額 2010-12-05 13:15:17.324 AddExpense [1179:207 ] IN - AddExpenseViewController :: hideDatePicker 2010-12-05 13:15:17.325 AddExpense [1179:207] IN - AddExpenseViewController :: textFieldShouldBeginEditing Amount 2010-12-05 13:15:17.326 AddExpense [1179:207] IN - AddExpenseViewController :: hideDatePicker 2010- 12-05 13:15:17.327 AddExpense [1179:207] IN - AddExpenseViewController :: textFieldShouldBeginEditing金額 2010-12-05 13:15:17.327 AddExpense [1179:207] IN - AddExpenseViewController :: hideDatePicker

和有是沒有辦法阻止它! 我的代碼有什麼問題?

由於提前,AM

回答

1

每當你做的UITextField firstReponder,textFieldShouldBeginEditing:發送到其委託。 一個臨時的解決辦法是

if (![textField.placeholder isEqual:[NSString stringWithFormat:@"Date"]]) { 
    [self hideDatePicker]; 
    textField.delegate = nil; 
    [textField becomeFirstResponder]; 
    textField.delegate = self; 
    return YES; 
} 

但我建議你重新設計你的代碼從該方法排除[textField becomeFirstResponder]

+0

嗨,我知道textFieldShouldBeginEditing:每次發送,但我不明白爲什麼它循環這個方法!我希望看到它只被稱爲一次,不要讓我的程序被鎖定... – wheisenberg 2010-12-05 13:10:56