UIPicker视图未填充

时间:2019-02-20 00:25:40

标签: ios swift uipickerview

我正在尝试填充3个选择器视图。由于某种原因,所有这些都显示为空白。知道为什么吗?

import UIKit

class ViewController2: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
    @IBOutlet weak var seconds: UIPickerView!
    @IBOutlet weak var minutes: UIPickerView!
    @IBOutlet weak var distance: UIPickerView!

    var meters = [200, 400, 600, 800, 1000, 1200]

    override func viewDidLoad() {
        super.viewDidLoad()
    }

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

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        switch pickerView {
        case minutes:
            return 100
        case seconds:
            return 59
        case distance:
            return (meters.count - 1)
        default:
            return 10
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        switch pickerView {
        case minutes:
            return String(row)
        case seconds:
            return String(row)
        case distance:
            return String(meters[row])
        default:
            return "ERROR 404"
        }
    }
}

1 个答案:

答案 0 :(得分:2)

viewDidLoad组中

self.seconds.delegate = self  
self.seconds.dataSource = self

以此类推,对于每个pickerView