UIPickerView不显示数组中的数据

时间:2019-04-05 11:21:39

标签: ios swift

我正在尝试做一个简单的选择器视图。这是到目前为止,我没有看到任何数据。我发现的大多数帮助都说将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])"
    }
}

问题是什么,我该怎么办?并谢谢你。

1 个答案:

答案 0 :(得分:3)

您好像忘记了delegate的{​​{1}}和dataSourceAddViewController的确认

因此在pickerUI方法中添加

viewDidLoad

您的方法将如下所示:

pickerUI.dataSource = self
pickerUI.delegate = self