0

I have 2 UIPickerViews as InputViews for textfields. I want to detect tap events on both of them. But only the taps on the first are detected.

Here is my code in ViewDidLoad:

    // for Behandler selection
    _behandlerArray = [self fetchBehandler];
    _behandlerPickerView = [[UIPickerView alloc] init];
    _behandlerTextField.inputView = _behandlerPickerView;
    _behandlerPickerView.dataSource = self;
    _behandlerPickerView.delegate = self;

    [_behandlerPickerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(behandlerPickerTapped:)]];

    // for Medikamente selection
    _medikamenteArray = [self fetchMedikamente];
    _medikamentePickerView = [[UIPickerView alloc] init];
    _medikamenteTextField.inputView = _medikamentePickerView;
    _medikamentePickerView.dataSource = self;
    _medikamentePickerView.delegate = self;

    [_medikamentePickerView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(medikamentePickerTapped:)]];

And here are the methods:

-(void)behandlerPickerTapped:(UIGestureRecognizer *)gestureRecognizer{
    NSLog(@"_behandlerPickerView tapped");
}

-(void)medikamentePickerTapped:(UIGestureRecognizer *)gestureRecognizer{
    NSLog(@"_medikamentePickerView tapped");
}

But only the taps on _behandlerPickerView are detected/logged.

Can I only added one UITapGestureRecognizer?

And if so, how do I know in the method, which view was tapped?

mrd
  • 4,561
  • 10
  • 54
  • 92
  • Tried your code on simple UIView, it works. The problem is not about having two UITapGestureRecognizer. Can you check that you don't have any overlay or other view above your second PickerView. – streem Apr 09 '14 at 11:42
  • No, not in the storyboard. But maybe the _behandlerPickerView, which is created in code as InputView of the _behandlerTextField overlays the _medikamentePickerView? If this would be the case, then one could never have more than one working UIGestureRecognizer on an InputView, because the InputViews are always handled by iOS itself and always positioned in the same area. Wouldn't that be a serious bug or bad design of iOS? Did you create your PickerViews as InputViews? – mrd Apr 09 '14 at 12:04
  • In your storyboard do you have the pickerviews inside the main view ? – streem Apr 09 '14 at 12:19
  • no, in the storyboard, I did not add the pickerviews, they are created by the code I have posted, I never add views, which are InputView of textfields to the storyboard. Where should I add them, they are only temporarily visible? – mrd Apr 09 '14 at 12:30
  • Well everything works fine for me with UIPickerViews. Does the content is different right now for your picker views ? what happens if you switch those contents. If you remove the first pickerview does the second works ? – streem Apr 09 '14 at 12:39
  • Now, this is really strange: I have had no rows on the _behandlerPickerView (The one which worked). I added a row, and detection stop working. Similiar, I removed all rows from the other PickerView and it started detecting taps. – mrd Apr 09 '14 at 12:51
  • I believe it is because its tap gesture is used on rows and it override yours. I'm not sure there is a clean way to solve this. Can't you use didSelectRow from the delegate of pickerview? – streem Apr 09 '14 at 12:58
  • No, i can't. If there is only a single row, it is not possible, because didSelectRow is only triggered when the selected row changes. In a pickerView with only a single row, this event is never triggered. – mrd Apr 09 '14 at 13:28
  • check this : http://stackoverflow.com/questions/22319427/ios-7-1-uitapgesture-not-working-with-uipickerview – streem Apr 09 '14 at 14:02
  • wow, big thanks! This link is the solution. And up to iOS 7.0 my old code worked, but since the upgrade to 7.1 the old code doesn't work anymore. – mrd Apr 09 '14 at 20:08

0 Answers0