-textFieldShouldBeginEditing is invoked for all textFields rather than when each textField is selected

436 Views Asked by At

I have an iPad app (XCode 6.1, iOS 8.1.1, ARC and Storyboards). In one of the classes (scene) I have this code:

-(BOOL) textFieldShouldBeginEditing:(UITextField *)textField  { //  (prevents keyboard from showing)

if(textField.tag == 200) {  //  birthdate
    [self showModalCalendar:(int)textField.tag];
    return NO;  //  don't show k/b
}
else
    return YES;

}

It is executed when the first textField is selected, going through each textField rather than wait until each textField is selected. This is a problem because what I want to accomplish is to show the modal calendar only when a particular UITextField (birthdate) has been selected, and NOT show the keyboard. What's happening is when I tap the tab key (on a hard keyboard) the modal calendar also apprears for each textField.

Is there any way to prevent this?

2

There are 2 best solutions below

1
Mike Gottlieb On

Rather than using tags you should create an IBOutlet to the particular textView that you want to handle. Then you can test if textView == your special one in the delegate callback.

The reason is that it is more explicit and clearer than using tags. My guess is that some of your other text views have that same tag for some reason and so your conditional isn't behaving as you think it should.

0
jcamacho On

The problem in my case was IQKeyboardManager library. I removed this library from my project and DownPicker works fine.