简体   繁体   English

以编程方式使用 UIPickerView 文本设置 UITextField 文本

[英]Set UITextField text with a UIPickerView text programmatically

I want to programmatically set the value of a UITextField with the content of a specific row of a UIPickerView (which is already its input).我想用 UIPickerView 的特定行的内容(已经是它的输入)以编程方式设置 UITextField 的值。

This has to be done without opening the UIPickeView since I want to set a default value once the PickerView's data source is populated at runtime.这必须在不打开 UIPickeView 的情况下完成,因为我想在运行时填充 PickerView 的数据源后设置默认值。

The solution I came with is this:我带来的解决方案是这样的:

pickerView.selectRow(0, inComponent: 0, animated: true)
textField.text = pvDataSource[ pickerView.selectedRow(inComponent: 0) ]

I want to know if there is a more elegant solution for it, like pretending the PickerView did show up and the user chose one of its options.我想知道是否有更优雅的解决方案,例如假装 PickerView 确实出现并且用户选择了其中一个选项。

I just checked some old source that I'd written where I have the same type of interaction.我刚刚检查了一些我写的旧来源,其中有相同类型的交互。

I have UIPickerView populated with some web resources, however it will default to a specific selection without any user interaction.我在UIPickerView填充了一些 Web 资源,但它会默认为特定选择,无需任何用户交互。

I have done exactly as you have done above.我已经完全按照你上面所做的做了。 Grab the 0'th component and resolve that back to my UI.获取第 0 个组件并将其解析回我的 UI。

I think that's as elegant as it gets with UIPickerViews.我认为这和 UIPickerViews 一样优雅。

Trying to wrap them in a further abstraction just isn't worth your time imo.试图将它们包装在进一步的抽象中是不值得的。

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

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