不符合协议" UIPickerViewDataSource"

时间:2017-07-04 15:42:53

标签: conform uipickerviewdatasource

为什么视图控制器不符合UIPickerViewDataSource?

我已经检查了我可以做些什么来从网上调试它。

我已经为协议键入了所有必要的功能(Swift 3.1)但是徒劳无功。

这是我的代码:

class SelectClassViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
    let grades = ["7", "8", "9", "10", "11", "12"]

    var selectCurrentGrade : String? = nil

    @IBOutlet weak var Grade: UIPickerView!

    @IBAction func Next(_ sender: Any) {
        if selectCurrentGrade != nil {
            performSegue(withIdentifier: "GradeToClass", sender: self)
        }
    }

    @IBAction func Back(_ sender: Any) {
        performSegue(withIdentifier: "GradeToMain", sender: self)
    }

    override func viewDidLoad() {
        super.viewDidLoad()

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

        func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
            return grades[row]
        }

        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return grades.count
        }

        func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
            selectCurrentGrade = grades[row]
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的功能位于viewDidLoad内,将其移到外面并且可以正常工作