xcode UIPickerView 获得“点击”元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3651957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIPickerView get "Clicked" Element?
提问by Phil
im using a rotated UIPickerView to use it horizontal. This works fine so far.
我使用旋转的 UIPickerView 水平使用它。到目前为止,这工作正常。
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.
现在我想获得“点击”的 UIPickerView 行的视图/标题。The Delegate gives me the opportunity to get the "selectedRow" when the Picker selects a row. 选择它时我不想要事件,我需要单击它的事件。
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.
我还尝试将按钮作为视图添加到 uipickerview。到目前为止,这有效,但我无法单击任何按钮。
Do i need a button as View in a UIPickerView row to get the touchupinside-event or can i use the normal row-element of a UIPickerView to get such an event?
我是否需要一个按钮作为 UIPickerView 行中的视图来获取 touchupinside 事件,或者我可以使用 UIPickerView 的普通行元素来获取这样的事件吗?
thx for any help!
感谢您的帮助!
回答by Adam Wallner
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.
您不需要继承 UIPickerView。
回答by Daniel
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).
当您单击一个元素时,它将被自动选中。因此, selecetedRow 方法将足够好,而且,它会在滑动时为您提供正确的元素(“单击”方法不会发生这种情况)。
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
要获取 UIPickerView 事件,您可能需要将控制器设置为符合 UIPickerViewDataSource 和 UIPickerViewDelegate 协议,并将选择器的委托和数据源设置为控制器。通过这种方式,您可以使用以下方法设置 pickerView 的内容
- (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:
回答by iOSPawan
u can customize the pickerView and inherit their touch event method.
你可以自定义pickerView并继承他们的触摸事件方法。
- (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];
}
回答by Perisheroy
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.
我最初通过在每个选择器项目中添加一个带有行索引作为标签的透明按钮来实现点击选择。它不再适用于 iOS 7,必须找到新的解决方案。