in my view I have several UITextFields and I need to show some times a keybord for text input and other times a UIDatePicker.
I order to show a DatePicker I implemented the delegate for the date UITextField, but once I select another UITextField for text input the DatePicker remains in background. Then I tried to implement the delegate also for another UITextField, with this code:
- (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;}
The problem is that when I select the date TextField and than the amount, the delegate gets called in loop:
2010-12-05 13:15:17.324 AddExpense[1179:207] IN – AddExpenseViewController::textFieldShouldBeginEditing Amount
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 Amount
2010-12-05 13:15:17.327 AddExpense[1179:207] IN – AddExpenseViewController::hideDatePicker
and there is no way to stop it !
What is wrong with my code ?
Thanks in advance, AM
Whenever you make UITextField firstReponder,
textFieldShouldBeginEditing:is sent to its delegate.A temporary solution could be
But I recommend you to redesign your code to exclude
[textField becomeFirstResponder]from that method.