Picker View 是显示字符“?”而不是项目名称?

Picker View is show character "?" instead of item name?

我对医院 ViewPicker 有疑问。当用户从 viewPicker 单击文本字段 hospitalName 到 select 医院名称时,该项目显示字符“?”我附上图片而不是项目名称 注意我对位置文本字段使用相同的逻辑

  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()
    }

您还没有设置 pickerHospital 委托。复制 self.pickerHospital.dataSource = self 两次。你应该写:

self.pickerHospital.dataSource = self
self.pickerHospital.delegate = self