I have the following class that I want to use as my UIPickerViewDelegate
class TimePickerViewDelegate: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
let pickerData = ["Mozzarella","Gorgonzola","Provolone","Brie","Maytag Blue","Sharp Cheddar","Monterrey Jack","Stilton","Gouda","Goat Cheese", "Asiago"]
//MARK: - Delegates and data sources
//MARK: Data Sources
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
//MARK: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
println( pickerData[row] )
}
}
I'm initializing it in my main UIViewController with the following
override func viewDidLoad() {
super.viewDidLoad()
let timePickerViewDelegate = TimePickerViewDelegate()
timePickerView.dataSource = timePickerViewDelegate
timePickerView.delegate = timePickerViewDelegate
}
However, when I execute the app, I get the following error.
2014-11-30 15:49:22.046 HausClock[78601:10273448] -[NSISEngine numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7da406e0
2014-11-30 15:49:22.052 HausClock[78601:10273448] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSISEngine numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7da406e0'
It seems to be telling me that my TimePicker View can't respond to the appropriate selector, but if I move the delegate from the NSObject to the UIViewController and copy/paste the delegate functions, it works correctly.
dataSource
anddelegate
are weak properties, you should declaretimePickerViewDelegate
as an instance property to hold a strong reference to the delegate.