下面的代码将更改所有 3 个组件的选取器视图的字体颜色。但是,当我尝试旋转车轮时它会崩溃。我认为这与 didSelectRow 函数有关。也许这两个函数必须以某种方式嵌套?任何想法?
func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
var attributedString: NSAttributedString!
if component == 0 {
attributedString = NSAttributedString(string: a.text!, attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
}
if component == 1 {
attributedString = NSAttributedString(string: b.text!, attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
}
if component == 2 {
attributedString = NSAttributedString(string: c.text!, attributes: [NSForegroundColorAttributeName : UIColor.redColor()])
}
return attributedString
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
switch component {
case 0:
aOutput.text = a[row] --> **Code breaks**
case 1:
bOutput.text = b[row]
case 2:
cOutput.text = c[row]
default:
10
}