如何获取 UIPickerView 中的分钟和秒如图所示?
我们如何实现这种类型的选定值在 分钟 内更改UIPickerView 中的秒???
How can we achieve such type of selected values to be changed with mins and secs in UIPickerView???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,使用
pickerView:viewForRow:inComponent:reusingView:
数据源方法返回“00”标签。这实际上有点棘手,因为 pickerview 会更改此视图的框架以填充行。这意味着您必须将正确定位的视图嵌入到另一个视图中,然后返回该容器视图。至于标签,这些只是已正确定位并添加为 pickerview 本身的子视图的
UILabel
对象。为了让它们正确更新,只需在
pickerView:didSelectRow:inComponent:
方法中更改它们的标题即可。这正是
UIDatePicker
正在做的事情。First, you use the
pickerView:viewForRow:inComponent:reusingView:
data source method to return the "00" label. This is actually a bit tricky, since pickerview will alter the frame of this view to fill the row. That means you have to embed your properly positioned view inside another view, and then return that container view instead.As for the labels, those are simply
UILabel
objects that have been positioned correctly and added as subviews of the pickerview itself.And to get them to update correctly, just change their titles in the
pickerView:didSelectRow:inComponent:
method.This is exactly what
UIDatePicker
is doing.您应该实现 UIPickerViewDelegate 协议和 UIPickerViewDataSource 协议。
在方法
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
中仅返回所需的标题。您还可以在
UIPickerView
的valueChanged
事件上添加委托人。每次您的值发生更改时,您都可以重新加载所有可见行并在正确的位置设置标签分钟和秒。哦,您可以在黑线上添加带有 2 个 UILabel 的子视图。
Oooor 您可以添加带有黑色渐变线和标签
mins
和secs
的子视图。然后使用方法:
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
您可以设置适当的列宽度。You should implement
UIPickerViewDelegate Protocol
andUIPickerViewDataSource Protocol
.In method
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
just return needful title.You can also add delegator on
valueChanged
event ofUIPickerView
. And each time your value changed you can reload all visible rows and set labels mins and secs at right positions.Oooor you can just add subview with 2
UILabel
s over the black line.Oooor you can add subview with black gradient line with labels
mins
andsecs
.Then using method :
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
you can set appropriate width of your columns.