我的下面的代码是一个 uitextfiled,它使用选择器 View 来显示 a 和 b。我想要做的就是让使用 socket 集合的所有文本字段显示相同的选择器 View 。 Textfield 是单个 textField,而 mulipleTextifeld 是我想要使用的 socket 集合。我只想用 mulipleTextifield 替换 textField。
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let picker = UIPickerView()
let country = ["a","b"]
@IBOutlet var mutlipleTextifeld: [UITextField]!
override func viewDidLoad() {
super.viewDidLoad()
for textFieldObject in mutlipleTextifeld
{
textFieldObject.inputView = picker
}}
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return country.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return country[row]
}
public func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
mutlipleTextifeld.text = country[row]
self.view.endEditing(false)
}}
最佳答案
在 viewDidLoad 方法中使用以下代码。就是这样。!
let count : int = 0
for textField in mutlipleTextifeld {
textField.inputView = picker
textField.tag = count
count +=1
}
在 Controller 中使用名称为 textFiledTag
的全局变量
每当您点击文本字段时,都会调用以下方法 -
textFieldDidBeginEditing
在这个方法中写入以下行
textFieldTag = textField.tag
现在您可以通过获取具有全局标记值的标签来设置选取器 View 的文本。
希望这有帮助..!
关于ios - 在 socket 集合文本字段上使用 pickerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46226918/