uipickerView显示为问号而不是数据?

时间:2014-11-04 23:34:33

标签: iphone swift xcode6

我正在尝试向我的iphone应用程序添加一个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]
    }
}

4 个答案:

答案 0 :(得分:19)

您设置了选择器视图的数据源,但未设置其委托。

结果pickerView:titleForRow:永远不会被调用。

答案 1 :(得分:0)

您应该在故事板中连接PickerView' DatasourceDelegate,或者以编程方式设置它们。见下图。

enter image description here

答案 2 :(得分:0)

New Swift 3已弃用上述方法,

我已实施以下内容以使其发挥作用。 (在参数中添加了一些下划线。

 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();
    }*/
}

答案 3 :(得分:0)

只需在viewDidLoad()menthod上添加 self.pickerOutletReference.delegate = self