这似乎太简单了,不足以作为一个帖子,但我已经坚持这个问题的时间比我想要的还要长。 pickerView不显示数组[durations]。运行时,模拟器只是空字段。代码是直接从UI类复制的,但似乎缺少某些东西。
class FirstViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var durationPicker: UIPickerView!
var durations = ["72 hours", "24 hours", "6 hours", "2 hours", "20 minutes"]
var durationsValue = [72.0, 24.0, 6.0, 2.0, 0.329]
var itemSelected: String = ""
var selectedDuration: Double = 0.0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return 5
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return durations[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
itemSelected = durations[row]
selectedDuration = durationsValue[row]
}
}
答案 0 :(得分:2)
您需要在delegate
内设置datasource
和viewDidLoad()
durationPicker.delegate = self
durationPicker.dataSource = self
或者只需使用Xcode界面
连接您的选择器delegate
和datasource
答案 1 :(得分:0)
我建议观看此视频。这位女士在解释各种iOS相关主题方面表现出色。
https://www.youtube.com/watch?v=MdXmIViD17U
跳出来的问题是你在顶部指定了数组。您应该使用return yourArray.count
返回Int
s。