im using a rotated UIPickerView to use it horizontal. This works fine so far.
Now i want to get the View/Title of the "clicked" UIPickerView row. The Delegate gives me the opportunity to get the "selectedRow" when the Picker selects a row. I want no event while selecting it i need the event of clicking it.
The funny thing is, i can already click the elements. i added no button or anything else. the "clicked" elements/row get highlighted like a button. But where should i attach the event listener? or how can i get any of this events?
i also tried to add buttons as view to the uipickerview. This works so far, but i can not click any button.
Do i need a button as View in a UIPickerView row to get the t开发者_如何学运维ouchupinside-event or can i use the normal row-element of a UIPickerView to get such an event?
thx for any help!
It's very easy:
- (void)viewDidLoad {
picker.userInteractionEnabled = YES;
UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(pickerTap)];
[picker addGestureRecognizer:tapGesture];
[tapGesture release];
}
- (void)pickerTap {
NSLog(@"Picker tapped");
}
You don't need to subclass UIPickerView.
When you click an element, it will be automatically selected. So, the selecetedRow method will be good enough, also, it will give you the right element when swiping (which will not happen with the "click" approach).
To get the UIPickerView events you may want to set your controller to conform to the UIPickerViewDataSource and UIPickerViewDelegate protocols and set the picker's delegate and datasource to the controller. This way you can set the content of the pickerView with methods like
- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
and get the event:
– selectedRowInComponent:
u can customize the pickerView and inherit their touch event method.
- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
if (!self.dragging) {
[self.nextResponder touchesEnded: touches withEvent:event];
}
//here u can keep track of touch event(click ,double click)
[super touchesEnded: touches withEvent: event];
}
check this post, it may not be the exact solution you are looking for, but it helps.
I originally implement the click to select by adding a transparent button with row index as tag in each picker item. It doesn't work any more on iOS 7 and have to find a new solution.
精彩评论