【问题标题】:UIPickerView get "Clicked" Element?UIPickerView 获得“点击”元素?
【发布时间】:2010-09-06 13:47:32
【问题描述】:

我使用旋转的 UIPickerView 水平使用它。 到目前为止,这工作正常。

现在我想获取“点击”的 UIPickerView 行的视图/标题。 当 Picker 时,Delegate 让我有机会获得“selectedRow” 选择一行。选择它时我不想要任何事件,我需要点击它的事件。

有趣的是,我已经可以点击元素了。我没有添加任何按钮或其他任何东西。 “点击”的元素/行像按钮一样突出显示。但是我应该在哪里附加事件监听器?或者我怎样才能得到这些事件?

我还尝试将按钮作为视图添加到 uipickerview。到目前为止,这有效,但我无法单击任何按钮。

我是否需要一个按钮作为 UIPickerView 行中的 View 来获取 touchupinside-event 或 我可以使用 UIPickerView 的普通行元素来获取这样的事件吗?

感谢任何帮助!

【问题讨论】:

    标签: iphone xcode uipickerview


    【解决方案1】:

    很简单:

    - (void)viewDidLoad {
        picker.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapGesture =[[UITapGestureRecognizer alloc] initWithTarget:self 
                                                                                            action:@selector(pickerTap)];
        [picker addGestureRecognizer:tapGesture];
        [tapGesture release];
    }
    
    - (void)pickerTap {
        NSLog(@"Picker tapped"); 
    }
    

    您不需要继承 UIPickerView。

    【讨论】:

      【解决方案2】:

      当你点击一个元素时,它会被自动选中。所以,selecetedRow 方法就足够了,而且,它会在滑动时为您提供正确的元素(“点击”方法不会发生这种情况)。

      要获取 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
      

      并获取事件:

      – selectedRowInComponent:
      

      【讨论】:

      • 谢谢回答,但我想这个对我没有多大帮助。用户应该使用 pickerview 选择一个值,然后或多或少地通过直接单击它来提交他的选择。也许我只是添加另一个按钮来提交,但我认为我可以使用行视图...
      【解决方案3】:

      你可以自定义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];
      }
      

      【讨论】:

      • 好的,我想这应该可行,但我需要一个扩展 uipickerview 的类,对吧?嗯,也许我真的应该构建一个自定义组件来解决这个问题,我只是认为它更容易,因为选择器获取事件。太糟糕了...
      【解决方案4】:

      检查this post,它可能不是您正在寻找的确切解决方案,但它会有所帮助。

      我最初通过在每个选择器项中添加一个带有行索引的透明按钮作为标签来实现单击选择。它在 iOS 7 上不再适用,必须寻找新的解决方案。

      【讨论】:

        猜你喜欢
        • 2022-01-21
        • 2016-05-13
        • 1970-01-01
        • 1970-01-01
        • 2021-08-12
        • 1970-01-01
        相关资源
        最近更新 更多