【问题标题】:Picker View is show character "?" instead of item name?选择器视图显示字符“?”而不是项目名称?
【发布时间】:2016-09-06 14:35:26
【问题描述】:

我对医院 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()
    }

【问题讨论】:

  • titleForRow添加打印;它打印正确的字符串吗?该字符串是可选的吗?
  • 我注意到你有 self.pickerHospital.dataSource = self 设置了两次。您能否删除其中一行并查​​看是否是导致问题的原因?

标签: ios swift swift2 ios9 uipickerview


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多