我正在尝试做一个简单的选择器视图。这是到目前为止,我没有看到任何数据。我发现的大多数帮助都说将pickerview代码放在viewDidLoad
下,但这不在我的主要ViewController
上,所以我不确定是否是问题所在。
import Foundation
import UIKit
class AddViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
let formulaNumbers = ["Formula1", "Formula2", "Formula3", "Formula4", "Formula5", "Formula6"]
let numberOfBatches = [1...100]
@IBOutlet weak var pickerUI: UIPickerView!
@IBAction func dissmissButton(_ sender:Any) {
self.dismiss(animated: true, completion: nil)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if component == 0 {
return formulaNumbers.count
}
return numberOfBatches.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if component == 0 {
return formulaNumbers[row]
}
return "\(numberOfBatches[row])"
}
}
问题是什么,我该怎么办?并谢谢你。
答案 0 :(得分:3)
您好像忘记了delegate
的{{1}}和dataSource
到AddViewController
的确认
因此在pickerUI
方法中添加
viewDidLoad
您的方法将如下所示:
pickerUI.dataSource = self
pickerUI.delegate = self