这是我的源代码,我所做的就是在storyBoard上做选择器视图。在此控制器中通过IBOutlet实现contorl+drag。
然而,它只能被汇编成“?”显示在“选择器”视图中。
问题出在哪里?
import UIKit
class SelectViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
var songNames = ["test1","test2","test3"]
@IBOutlet weak var songPicker: UIPickerView!
override func viewDidLoad(){
songPicker.delegate = self
songPicker.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return songNames.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int) -> String? {
return songNames[row]
}
}发布于 2017-05-18 15:15:29
您忽略了forComponent方法中的dataSource参数。将其添加到titleForRow函数中,如下所示:
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return songNames[row]
}这应该能解决你遇到的问题。
https://stackoverflow.com/questions/44051485
复制相似问题