我有一个带有以下代码的工作uipickerview
class DateTableViewController: UITableViewController, UIPickerViewDelegate, UIPickerViewDataSource {
var colors: [String] = ["red","green","blue"]
override func viewDidLoad() {
super.viewDidLoad()
myPicker = UIPickerView()
myPicker.dataSource = self
myPicker.delegate = self
self.view.userInteractionEnabled = true
var myGR = UITapGestureRecognizer(target: self, action: "pickerTapped:")
myPicker.addGestureRecognizer(myGR)
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return colors.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return colors[row] as! String
}
func pickerTapped(sender : UIView) {
println("Tap Gesture recognized")
}
}
不幸的是,当我点击选择器查看项目时没有任何反应,有人可以帮我这个吗?
答案 0 :(得分:0)
选择器视图已经处理'点击以选择'行为,允许您在PickerView委托协议中指定的DidSelectRow
方法中实现选择逻辑。
取消手势识别器并用以下代码替换pickerTapped
函数:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
println("\(colors[row]) was selected")
}
您的点击手势识别器未触发的原因是Picker View已经处理了点击手势以管理选择,并且在进行选择时将通知其代表。您无需手动处理此选择。