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] )
}
}
メインの UIViewController で次のように初期化しています
override func viewDidLoad() {
super.viewDidLoad()
let timePickerViewDelegate = TimePickerViewDelegate()
timePickerView.dataSource = timePickerViewDelegate
timePickerView.delegate = timePickerViewDelegate
}
ただし、アプリを実行すると、次のエラーが発生します。
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'
TimePicker ビューが適切なセレクターに応答できないことを教えてくれているようですが、デリゲートを NSObject から UIViewController に移動し、デリゲート関数をコピーして貼り付けると、正しく機能します。