Does not conform to protocol "UIPickerViewDataSource"

589 Views Asked by At

Why doesn't the view controller conform the UIPickerViewDataSource?

I have checked everything I could do to debug it from the web.

I have typed all the necessary functions for the protocols (Swift 3.1) but in vain.

Here is my code:

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

There are 1 best solutions below

0
David Liaw On

Your functions are inside viewDidLoad, move them outside and it'll work