我的视图中有两个不同的 UIPickerView。当我通过情节提要将数据源和委托设置为它们所在的视图时,它们工作得很好,但是当我尝试通过如下所述的代码来执行此操作时,它不起作用。
两个选择器应该有不同的数据来显示(甚至可能代表不同的行为)。因此,我想以编程方式将它们连接到不同的数据源。
我尝试创建自己的类来实现 UIPickerViewDataSource- 和 UIPickerViewDelegate-Protocols 并将该类的对象连接到我的 PickerViews,但它不起作用。运行时抛出异常,terminating with uncaught exception of type NSException
说明:
2015-01-09 17:50:05.333 Pet Stats[4953:244338] -[NSConcreteMapTable numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7b4616d0
2015-01-09 17:50:05.338 Pet Stats[4953:244338] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteMapTable numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7b4616d0'
我怎样才能让它工作?我错过了什么?这是我的代码:
WeightWheelController.swift
import UIKit
class WeightWheelController: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {
let ElementCount: Int!
init(pickerInterval: Int) {
ElementCount = pickerInterval
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return ElementCount
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return String(row + 1)
}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, inComponent component: Int)
{
println("External Controller:" + String(row + 1))
}
}
WeightWheelInputViewController.swift
import UIKit
class WeightWheelInputViewController: UIViewController {
@IBOutlet weak var picker1: UIPickerView!
@IBOutlet weak var picker2: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
//picker attached to c1 should show number from 1 to 150
let c1 = WeightWheelController(pickerInterval: 150)
//picker attached to c1 should show number from 1 to 10
let c2 = WeightWheelController(pickerInterval: 10)
picker1.dataSource = c1
picker1.delegate = c1
picker2.dataSource = c2
picker2.delegate = c2
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
简要更新:
在这个问题中,我发现您可以为不同的选择器视图使用不同的标签。那将是一种选择;然而,我不喜欢它。我宁愿遵循 MVC'ish 方法并将不同的控制器连接到每个选择器。这不可能吗?