我想同时标记文本字段ana和sampleTextfield。这样,在扩展字段中,我可以做一些事情,例如为每个textfireld调用不同的pickerview。示例:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if tag ==1 {
return firstHit[row]
} else {
return sendHit[row]
}
}
代码:
var ana = UITextField(frame: CGRect(x: -20, y: 100, width: 300, height: 40))
var sampleTextField = UITextField(frame: CGRect(x: 20, y: 100, width: 300, height: 40))
extension ViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return firstHit.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return firstHit[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selecteDay = firstHit[row]
sampleTextField.text = selecteDay
}
func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
var lable: UILabel
if let view = view as? UILabel {
lable = view
} else {
lable = UILabel()
}
lable.textColor = .white
lable.textAlignment = .center
lable.font = UIFont(name: "Menlo-Regular", size: 17)
lable.text = firstHit[row]
return lable
}
}