0

How can I detect when the user taps the selection indicator in a UIPickerView?

Without this the user has to scroll to some other row and then back again to pick the value which is displayed under the selection indicator when the picker slides up.

Thanks a lot,
Stine

enter image description here

UPDATE: Currently getting it to work by using my own SensitivePickerView (got the idea here Responding to touchesBegan in UIPickerView instead of UIView):

#import <UIKit/UIKit.h>

@protocol SensitivePickerViewDelegate <UIPickerViewDelegate>
- (void) pickerViewWasTouched;
@end

@interface SensitivePickerView : UIPickerView

@property (nonatomic, assign) id<SensitivePickerViewDelegate> delegate;

@end

... and the implementation:

#import "SensitivePickerView.h"

@interface SensitivePickerView ()
- (UIView *) getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event;
@end

@implementation SensitivePickerView

@synthesize delegate;

- (void) setDelegate:(id<SensitivePickerViewDelegate>)aDelegate {
    [super setDelegate:aDelegate];
    delegate = aDelegate;
}

- (UIView *) hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return self;
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView *hitTestView = [self getNextResponderView:touches withEvent:event];
    [hitTestView touchesBegan:touches withEvent:event];
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView *hitTestView = [self getNextResponderView:touches withEvent:event];
    [hitTestView touchesMoved:touches withEvent:event];
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView *hitTestView = [self getNextResponderView:touches withEvent:event];
    [hitTestView touchesEnded:touches withEvent:event];
    [self.delegate pickerViewWasTouched];
}

- (void) touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    UIView *hitTestView = [self getNextResponderView:touches withEvent:event];
    [hitTestView touchesCancelled:touches withEvent:event];
}

- (UIView *) getNextResponderView:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView:self];
    UIView *hitTestView = [super hitTest:point withEvent:event];
    return (hitTestView == self) ? nil : hitTestView;
}

@end

Not ideal at all but it works :/

Community
  • 1
  • 1
Stine
  • 1,605
  • 5
  • 23
  • 44
  • closely related: http://stackoverflow.com/questions/7455310/detect-when-user-taps-the-selection-indicator-in-a-uidatepicker – albertamg Sep 17 '11 at 16:33
  • Sorry i did not get your question completely – Praveen-K Sep 17 '11 at 16:55
  • @albertamg: yes, they are very closely related. The problem is that the solution I found in the `UIDatePicker` case does not at all work in the `UIPickerView` case. – Stine Sep 17 '11 at 17:00
  • @Praveen-K: I would like to detect when the user touches the value already highlighted in the picker. In the example from the image included in my question: if the user touches/taps the "1 minutes" in the picker view I would like to know such that I can replace the placeholder "Required" to the right of "Duration" with "1 minutes". – Stine Sep 17 '11 at 17:04

1 Answers1

0

If you want some default value to be displayed, call - [picker selectRow:row inComponent:component animated:NO] initializing your picker.
Afraid, you have no way to know if user touches already picked value an doesn't changes it.

Valeriy Van
  • 1,851
  • 16
  • 19
  • Thanks, but not exactly what I was after :/ – Stine Sep 17 '11 at 17:24
  • Stive, if you will think a little more you agree it is the only possible functionality. – Valeriy Van Sep 17 '11 at 17:31
  • You can not do with UIPickerView, there is no way to when the user touches the value already highlighted in the picker. – Praveen-K Sep 17 '11 at 18:27
  • @Praveen-K: That is what I have realized as well by now. Using my `SensitivePickerView` mentioned above I can obtain something similar though. – Stine Sep 17 '11 at 18:43