Swift - 无法使 PickerView 库正常工作

Swift - Cannot make PickerView Library to Work

我正在尝试将这个库集成到我的项目中

https://github.com/filipealva/PickerView

按照指示,我在界面生成器中添加了一个 UIView 并创建了到该视图的 @IBOutlet 连接。

这是我控制器的代码

import UIKit
import PickerView

class SignUpStep1ViewController: UIViewController {

    @IBOutlet weak var nextBtn: UIButton?
    @IBOutlet weak var dayPicker: PickerView!

    var months = [
        "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
    ]

    override func viewDidLoad() {
        super.viewDidLoad()
        dayPicker.delegate = self
        dayPicker.dataSource = self
        configureDayPicker()
        title = "BIRTH DATE"
    }

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.nextBtn?.layer.cornerRadius = 22.5
    }

    @IBAction func nextBtnClicked(sender: UIButton) {
        performSegue(withIdentifier: "SignUpStep2ViewController", sender: self)
    }

    fileprivate func configureDayPicker() {
        print("DayPicker Configuration")
        dayPicker.scrollingStyle = .default
        dayPicker.backgroundColor = UIColor.red
        dayPicker.translatesAutoresizingMaskIntoConstraints = false
        dayPicker.scrollingStyle = PickerView.ScrollingStyle(rawValue: 1)!
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        let backItem = UIBarButtonItem()
        backItem.title = ""
        navigationItem.backBarButtonItem = backItem
    }

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

extension SignUpStep1ViewController: PickerViewDataSource {

    func pickerViewNumberOfRows(_ pickerView: PickerView) -> Int {
        return months.count
    }

    func pickerView(_ pickerView: PickerView, titleForRow row: Int, index: Int) -> String {
        return months[index]
    }
}

extension SignUpStep1ViewController: PickerViewDelegate {

    func pickerViewHeightForRows(_ pickerView: PickerView) -> CGFloat {
        return 50.0
    }

    func pickerView(_ pickerView: PickerView, didSelectRow row: Int) {

    }

    func pickerView(_ pickerView: PickerView, didTapRow row: Int, index: Int) {

    }

    func pickerView(_ pickerView: PickerView, styleForLabel label: UILabel, highlighted: Bool) {
        if highlighted {
            label.font = UIFont.systemFont(ofSize: 25.0)
            label.textColor = view.tintColor
        } else {
            label.font = UIFont.systemFont(ofSize: 15.0)
            label.textColor = .lightGray
        }
    }
}

这表明在控制器中创建了 IBOutlet 连接,但是视图中没有显示选择器,这是哪里出了问题?

这是故事板视图的屏幕截图。

您得到的是红色背景颜色,这意味着正在显示 pickerview(红色背景是在 configureDayPicker 方法中设置的)。我认为您没有收到文本,因为您使用 index 而不是 row 来访问数组中的值。尝试改变

func pickerView(_ pickerView: PickerView, titleForRow row: Int, index: Int) -> String {
        return months[index]
}

func pickerView(_ pickerView: PickerView, titleForRow row: Int, index: Int) -> String {
        return months[row]
}

编辑: 另外,尝试将文本颜色设置为对比色以确认数据是否确实正在加载(色调颜色可能会将文本混合到背景中)