挑选者采取行动改变

Gho*_*108 7 uipickerview ios swift ios8

我想更改标签,而选择器视图的值已更改.

问题是我无法通过将选择器视图放到代码中来创建操作.这不可能吗?

使用我的datepicker,我可以这样做:

    @IBAction func ChangeDatePicker(sender: AnyObject) {

    var dateFormatter = NSDateFormatter()
    dateFormatter.dateStyle = NSDateFormatterStyle.MediumStyle
    dateFormatter.dateFormat = "HH:mm"
    var strDate = dateFormatter.stringFromDate(DatePicker.date)

    DateLabel.text = "\(strDate) Uhr"
}
Run Code Online (Sandbox Code Playgroud)

小智 10

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    print(myPickerViewData[row])
}
Run Code Online (Sandbox Code Playgroud)

这里的教程:http://makeapppie.com/tag/uipickerview-in-swift/


muh*_*sva 0

您必须以这种方式设置操作。如果选择器值发生更改,它将调用您的函数。

DatePicker.addTarget(self, action: Selector("ChangeDatePicker:"), forControlEvents: .ValueChanged)
Run Code Online (Sandbox Code Playgroud)

  • Target-action 仅适用于 UIControl,UIPickerView 似乎不是 UIControl。但 UIDatePicker 是,你可以从 Apple 文档中确认。在我看来,UIPickerView 应该是一个 UIControl 是合理的。不知道为什么他们不这样做。 (2认同)