简体   繁体   English

滚动UIPickerView时如何实时获取所选值

[英]How can I get the selected value in the real-time, when I'm scrolling the UIPickerView

这是我的原型 For example, in this image, when I'm scrolling the UIPickerView to 2012 9 28 , what I want is that the text of the black label will change into 2012 9 28 at the same time without pressing any buttons like the Done button 例如,在此图像中,当我将UIPickerView滚动到2012 9 28我想要的是黑色标签的文本将同时变为2012 9 28 而无需按“完成”按钮之类的任何按钮。

I'm using UIPickerView, I can get the selected data before, I can also put the data into label by clicking a Done button, but I cannot put the data into the label when I'm scrolling. 我使用的是UIPickerView,可以先获取选定的数据,也可以通过单击“完成”按钮将数据放入标签中,但是在滚动时无法将数据放入标签中。

and In a general situation, My question is that when I Scroll the UIPickerView, how can I get the data which is selected in real time 在一般情况下,我的问题是,当我滚动UIPickerView时,如何获取实时选择的数据

could anyone help me ? 有人可以帮我吗? ObjectiveC solution is OK, Swift solution is better for me, Thank you so much ObjectiveC解决方案还可以,Swift解决方案对我来说更好,非常感谢

It is unclear if you are using a UIPickerView or a UIDatePicker. 目前尚不清楚您使用的是UIPickerView还是UIDatePicker。

For UIPickerView you need to implement the UIPickerViewDelegate. 对于UIPickerView,您需要实现UIPickerViewDelegate。 Make sure that delegate is added to your ViewController declaration and make sure in Storyboard to connect the delegate of the UIPickerView control to your view controller. 确保将委托添加到ViewController声明中,并确保在Storyboard中将UIPickerView控件的委托连接到视图控制器。 Then implement this function: 然后实现此功能:

func pickerView(_ pickerView: UIPickerView, 
        didSelectRow row: Int, 
         inComponent component: Int) {

}

For UIDatePicker you need to connect the action of the UIDatePicker in Storyboard to an @IBAction function in your view controller or else connect it in code using the addTarget function: 对于UIDatePicker,您需要将Storyboard中的UIDatePicker的操作连接到视图控制器中的@IBAction函数,或者使用addTarget函数将其连接到代码中:

myDatePicker.addTarget(self, action: #selector(self.respondToPicker, for: .valueChanged), 

Let me suppose that you are using UIDatePicker , in that you can control the action using UIControlEventValueChanged 让我假设您正在使用UIDatePicker ,因为您可以使用UIControlEventValueChanged来控制操作

Like, 喜欢,

datePickerView?.addTarget(self, action: #selector(self.valueChanged(_:)), for: UIControlEvents.valueChanged)

the valueChanged() will be, valueChanged()将是

func valueChanged(_ datePicker: UIDatePicker) {
    let selectedDate = datePicker.date as NSDate
    print(selectedDate)
}

and if you are using UIPickerview then, 如果您使用的是UIPickerview

titleForRow : will gave you scrolling value titleForRow :将为您提供滚动值

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
    print("your value")
}

and didSelectRow : will give you selected value didSelectRow :将为您提供选定的值

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
     print("your value")
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM