UIPickerView
显示数据,但不传输到UITextField
。
我检查了其他帖子以比较我的代码,但仍然找不到解决方案。非常感谢您的帮助。
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
@IBOutlet weak var hospNameField: UITextField!
@IBOutlet weak var teamNameField: UITextField!
@IBOutlet weak var mdNameField: UITextField!
@IBOutlet weak var selectionsPickerView: UIPickerView!
var hosp = ["hosp1", "hosp2", "hosp3"]
var team = ["team1", "team2", "team3"]
var mds = ["MD1", "MD2", "MD3"]
var itemSelected = ""
var textFieldSelected = UITextField()
weak var pickerView: UIPickerView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
hospNameField.inputView = pickerView
teamNameField.inputView = pickerView
mdNameField.inputView = pickerView
hospNameField.delegate = self
teamNameField.delegate = self
mdNameField.delegate = self
self.pickerView = pickerView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldSelected = textField
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if hospNameField.isFirstResponder {
return hosp.count
} else if teamNameField.isFirstResponder {
return team.count
} else if mdNameField.isFirstResponder {
return mds.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if hospNameField.isFirstResponder {
return hosp[row]
} else if teamNameField.isFirstResponder {
return team[row]
} else if mdNameField.isFirstResponder {
return mds[row]
}
return nil
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if teamNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
}
}
答案 0 :(得分:0)
我认为您的代码在didSelectRow
中是错误的。检查第一个if
和第二个if teamNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
,它们都运行相同的条件。
if
我认为您应该将第一个if hospNameField.isFirstResponder {..}
更改为element(by.cssContainingText('ion-button', 'Cancel appointment '));
答案 1 :(得分:0)
您忘记了将代理和数据源订阅到PickerView。
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
@IBOutlet weak var hospNameField: UITextField!
@IBOutlet weak var teamNameField: UITextField!
@IBOutlet weak var mdNameField: UITextField!
@IBOutlet weak var selectionsPickerView: UIPickerView!
var hosp = ["hosp1", "hosp2", "hosp3"]
var team = ["team1", "team2", "team3"]
var mds = ["MD1", "MD2", "MD3"]
var itemSelected = ""
var textFieldSelected = UITextField()
weak var pickerView: UIPickerView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
hospNameField.inputView = pickerView
teamNameField.inputView = pickerView
mdNameField.inputView = pickerView
hospNameField.delegate = self
teamNameField.delegate = self
mdNameField.delegate = self
self.pickerView = pickerView
/**
the below lines are mandatory for
pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
to execute
*/
self.pickerView?.delegate = self
self.pickerView?.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldSelected = textField
}
// add this delegate and reload here
func textFieldShouldBeginEditing(_ textField: UITextField) ->
Bool {
self.pickerView?.reloadAllComponents()
return true
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if hospNameField.isFirstResponder {
return hosp.count
} else if teamNameField.isFirstResponder {
return team.count
} else if mdNameField.isFirstResponder {
return mds.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if hospNameField.isFirstResponder {
return hosp[row]
} else if teamNameField.isFirstResponder {
return team[row]
} else if mdNameField.isFirstResponder {
return mds[row]
}
return nil
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
/**
type here, need to check the responder for hospNameField also
you have executed teamNameField.isFirstResponder twice
*/
if hospNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
}
}