我对医院ViewPicker有疑问。当用户点击文本字段hospitalName从viewPicker中选择医院名称时,项目显示字符“?”而不是项目名称我附加了图像 注意我对位置文本字段使用相同的逻辑
class VisitsViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate,UITextFieldDelegate, UIViewControllerTransitioningDelegate {
//MARK -
override func viewDidLoad() {
super.viewDidLoad()
self.view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(BrnViewController.dismissKeybored)))
self.pickerLocation.dataSource = self
self.pickerLocation.delegate = self
self.pickerLocation.tag = 1
self.pickerHospital.dataSource = self
self.pickerHospital.dataSource = self
self.pickerHospital.tag = 2
visitLocationTextField.inputView = pickerLocation
self.visitLocationTextField.delegate = self
hospitalNameTextField.inputView = pickerHospital
self.hospitalNameTextField.delegate = self
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: - Picker View
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1 {
return pickerDataVisitLocation.count
}
else{
return pickerDataHospitalName.count
}
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
if pickerView.tag == 1{
visitLocationTextField.text = pickerDataVisitLocation[row]
}
else{
HospitalNameTextField.text = pickerDataHospitalName[row]
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int)-> String? {
if pickerView.tag == 1{
return pickerDataVisitLocation[row]
}
else{
return pickerDataHospitalName[row]
}
}
//MARK: - @IBOutlet
@IBOutlet weak var visitLocationTextField: UITextField!
@IBOutlet weak var hospitalNameTextField: UITextField!
//MARK: - variables
var pickerDataVisitLocation = [" ","Home","Hospital","Other"]
var pickerDataHospitalName = [" ","test1","test2","test3"]
var pickerLocation = UIPickerView()
var pickerHospital = UIPickerView()
}
答案 0 :(得分:1)
您还没有设置pickerHospital委托。复制self.pickerHospital.dataSource = self
两次。你应该写道:
self.pickerHospital.dataSource = self
self.pickerHospital.delegate = self