UIPickerView仅显示问号

时间:2016-12-30 12:47:55

标签: ios swift3 uipickerview

当我向我们提问时,以下代码仅在PickerView中看到问号。 我找不到错误。

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {


    @IBOutlet weak var dbPicker: UIPickerView!

    var dbPickerData: [String] = [String]()


    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        //Connect Data:
        self.dbPicker.dataSource = self
        self.dbPicker.delegate = self

        // Fill dbPicker content
        dbPickerData = ["White","Red","Green","Blue"]
    }

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

    // The number of columns of data
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1;
    }

    // The number of rows of data
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return dbPickerData.count;
    }

    // The data to return for the row and component (column) that's being passed in
    private func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return dbPickerData[row];
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        if(row == 0)
        {
            self.view.backgroundColor = UIColor.white;
        }
        else if(row == 1)
        {
            self.view.backgroundColor = UIColor.red;
        }
        else if(row == 2)
        {
            self.view.backgroundColor =  UIColor.green;
        }
        else
        {
            self.view.backgroundColor = UIColor.blue;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

而不是这个

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

用这个更新它

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        let str: String = dbPickerData[row]
        return str
    }

希望它会有所帮助...