pickerView shows up as question marks instead of data?

独自空忆成欢 提交于 2019-12-08 16:31:51

问题


I am trying to add a pickerview to my iphone application but instead of showing string from my array it shows question marks. Does anyone know why? I've been trying to figure it out for the past hour... Here is my code of the controller that contains the pickerview:

class NewIssueViewController: UIViewController, UIPickerViewDelegate {

    var componentArr = ["component1","component2","component3","component4"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBAction func CancelPressed(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    @IBAction func AddPressed(sender: AnyObject) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
        return 1
    }

    func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
        return componentArr.count
    }

    func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String! {
        return componentArr[row]
    }
}

回答1:


You set your picker view's datasource, but didn't set its delegate.

As a result pickerView:titleForRow: never gets called.




回答2:


You should either connect the PickerView's Datasource and Delegate in the storyboard or set them programatically. See the image below.




回答3:


New Swift 3 has deprecated usage of above methods,

i have implemented the following to make them work. (Some underscore added in param.

 func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerDataSource.count;
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return pickerDataSource[row]
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
    /*if(row == 0)
    {
        self.view.backgroundColor = UIColor.whiteColor();
    }
    else if(row == 1)
    {
        self.view.backgroundColor = UIColor.redColor();
    }
    else if(row == 2)
    {
        self.view.backgroundColor =  UIColor.greenColor();
    }
    else
    {
        self.view.backgroundColor = UIColor.blueColor();
    }*/
}



回答4:


Just add self.pickerOutletReference.delegate = self on viewDidLoad() menthod



来源:https://stackoverflow.com/questions/26747115/pickerview-shows-up-as-question-marks-instead-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!