UIPicker阵列数据将不会显示

时间:2016-03-26 23:54:58

标签: ios arrays xcode uitextfield uipickerview

我没有错误,但又遇到了这个问题!

我的UIPickerViews不会使用其阵列中的相应数据填充。当我"建立并运行" UIPicker弹出,但" climateOption"和" environmentOption"单击UITextField时不要填充UIPicker。这是我的代码:感谢大家的帮助。

@IBOutlet weak var climateTextField: UITextField!
@IBOutlet weak var environmentTextField: UITextField!


var climateOption = ["Hot", "Warm", "Cold", "Anywhere"]
var environmentOption = ["Beach","City","Mountain","Rural","Anywhere"]

override func viewDidLoad() {
    super.viewDidLoad()

let pickerView = UIPickerView()
    pickerView.delegate = self

        climateTextField.inputView = pickerView
        environmentTextField.inputView = pickerView

climateTextField.inputAccessoryView = toolBar
environmentTextField.inputAccessoryView = toolBar

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == climateTextField {
        return climateOption.count
    } else if pickerView == environmentTextField {
        return environmentOption.count
    }
        return 1
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == climateTextField {
        return climateOption[row]
    } else if pickerView == environmentTextField {
        return environmentOption[row]
    }
        return ""
}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == climateTextField{
        climateTextField.text = climateOption[row]
    } else if pickerView == environmentTextField{
        environmentTextField.text = environmentOption[row]
    }

}


}

0 个答案:

没有答案