UIPickerView无法加载

时间:2018-04-28 21:02:05

标签: swift uipickerview

这个UIPicker的数据没有加载,我得到正确的行数,但是“?”对于每个元素而不是数组pickerValues中的值。我是Swift的新手,希望我错过了一些明显的东西。 Xcode 9 / Swift4。

import UIKit


class ViewController3: UIViewController,UIPickerViewDelegate, UIPickerViewDataSource {

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return 4
    }

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


    @IBOutlet weak var picker: UIPickerView!

    let pickerValues = ["Choice 1", "Choice 2", "Choice 3","Choice 4"]


    override func viewDidLoad() {
        super.viewDidLoad()

        picker.dataSource = self

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

}

编辑:我不同意我的问题是一个骗局。它处理相同的主题,但在我的情况下,缺少选择器委托方法。链接问题不是这样。

1 个答案:

答案 0 :(得分:1)

您需要实施

func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String?  {

    return pickerValues[row]

}