UIPickerView didSelectRow未调用

时间:2014-06-26 16:57:51

标签: ios xcode delegates swift uipickerview

(Swift,Xcode6,iOS8,iPhone)

在我的UIPickerView控件中,didSelectRow未被调用。

  • 是的,我已将Splash设置为选择器的委托(和数据源)。
  • Splash扩展了UIViewController
  • 所有其他功能都有效。没有错误,选择器旋转,有五行,并显示其他函数中定义的行号。

何时调用didSelectRow方法?我是否需要实现某种Done按钮,或者当用户停止旋转时此方法是否会触发?我需要实施单独的通知吗? tyvm:)

class Splash: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet var countryPicker : UIPickerView = nil

func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
    return 1
}
func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
    return 5
}
func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String {
    return "\(row)"

}
func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, forComponent component: Int) -> Int {
    println("Row: \(row)")
    return row
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
}

2 个答案:

答案 0 :(得分:1)

您的签名错误,pickerView:didSelectRow:forComponent:返回void,而不是整数。尝试:

func pickerView(pickerView: UIPickerView!, didSelectRow row: Int, forComponent component: Int) {
    println("Row: \(row)")
}

答案 1 :(得分:0)

迅速4.2

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("selected row = \(row)") }