当我滚动 UIPickerView 时,如何实时获取选定的值 [英] How can I get the selected value in the real-time, when I'm scrolling the UIPickerView

查看:85
本文介绍了当我滚动 UIPickerView 时,如何实时获取选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,在这张图片中,当我将 UIPickerView 滚动到 2012 9 28 时,我想要的是黑色标签的文本将变为 2012 9 28 同时没有按下任何按钮,如完成按钮

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,我之前可以获取选定的数据,我也可以通过单击完成"按钮将数据放入标签中,但是我无法将数据放入标签中'm 滚动.

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

有人可以帮我吗?ObjectiveC 解决方案还可以,Swift 解决方案更适合我,非常感谢

could anyone help me ? ObjectiveC solution is OK, Swift solution is better for me, Thank you so much

推荐答案

不清楚您使用的是 UIPickerView 还是 UIDatePicker.

It is unclear if you are using a UIPickerView or a UIDatePicker.

对于 UIPickerView,您需要实现 UIPickerViewDelegate.确保将委托添加到您的 ViewController 声明中,并确保在 Storyboard 中将 UIPickerView 控件的委托连接到您的视图控制器.然后实现这个功能:

For UIPickerView you need to implement the 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. Then implement this function:

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

}

对于 UIDatePicker,您需要将 Storyboard 中 UIDatePicker 的操作连接到视图控制器中的 @IBAction 函数,或者使用 addTarget 函数在代码中连接它:

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:

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

这篇关于当我滚动 UIPickerView 时,如何实时获取选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆