I get this error: /Users/natumyers/Desktop/proj/SignUp2ViewController.swift:11:7: Type 'SignUp2ViewController' does not conform to protocol 'UIPickerViewDataSource'. I am following this tutorial http://codewithchris.com/uipickerview-example/ but I have a view controller that's separate from the default one.
import UIKit
class SignUp2ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var signuplabel: UITextField!
var labelText = String()
@IBOutlet weak var focusPicker: UIPickerView!
var focusPickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
// Connect data:
self.focusPicker.delegate = self
self.focusPicker.dataSource = self
focusPickerData = ["type1","type2","type3","type4"]
signuplabel.text = labelText
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Solution
The functions were key for this to work. I added:
// The number of columns of data
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
// The number of rows of data
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return focusPickerData.count
}
// The data to return for the row and component (column) that's being passed in
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return focusPickerData[row]
}
After the didReceiveMemoryWarning function.
Your view controller does not conform to UIPickerViewDataSource protocol. You need to implement these two required methods:
numberOfComponentsInPickerView(_:)
pickerView(_:numberOfRowsInComponent:)
The data source provides the picker view with the number of components, and the number of rows in each component, for displaying the picker view data. Both methods in this protocol are required.