我生成的代码有什么问题?我试图让一个UIPickerView
弹出时,文本字段被点击,然后走开,一旦作出选择。文本字段应该显示之后选择的内容。
import UIKit
class CreateAJob_View_ControllerViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet var pickerTextField: UITextField!
@IBOutlet var picker: UIPickerView!
var pickerData:[String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
picker.hidden = true;
pickerTextField.text = "A4";
self.pickerTextField.delegate = self
self.picker.delegate = self
self.picker.dataSource = self
pickerData = ["A0","A1","A2","A3","A4","A5","A6","A7","A8","A9","A10"]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerTextField.text = pickerData[row];
picker.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
pickerTextField.hidden = false
return false
}
}
最佳答案
您需要这样更改这两种方法:
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.pickerTextField.hidden = false;
self.picker.hidden = true;
self.pickerTextField.text = pickerData[row];
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
self.pickerTextField.hidden = true
self.picker.hidden = false;
return false
}