我一直在搜索论坛,没有任何帮助。 我在一个视图控制器中使用4个文本字段,对于每个文本字段,我使用单独的pickerView作为textFields(4pickers)的inputViews。
当我点击第一个textField时,pickerView1成功出现,textfield显示数据,但是当我点击第二,第三和第四个文本字段时,会出现第一个pickerView。 我怀疑错误在于inputView声明。
如果你能帮助在pickerView中添加一个“完成”按钮,我将非常感激。
我的代码:
class ViewController1: UIViewController, UIPickerViewDelegate
{
@IBOutlet var pickerView1: UIPickerView!
@IBOutlet var pickerView2: UIPickerView!
@IBOutlet var pickerView3: UIPickerView!
@IBOutlet var pickerView4: UIPickerView!
@IBOutlet var textField1: UITextField!
@IBOutlet var textField2: UITextField!
@IBOutlet var textField3: UITextField!
@IBOutlet var textField4: UITextField!
var hazards = ["a","b", "c"]
var reasons = ["d", "e", "f"]
var site = ["v","h","i","j"]
var line = ["k", "l","m", "n"]
override func viewDidLoad() {
super.viewDidLoad()
pickerView1 = UIPickerView()
pickerView2 = UIPickerView()
pickerView3 = UIPickerView()
pickerView4 = UIPickerView()
pickerView1.delegate = self
pickerView2.delegate = self
pickerView3.delegate = self
pickerView4.delegate = self
self.textField1.inputView = self.pickerView1;
self.textField2.inputView = self.pickerView2;
self.textField3.inputView = self.pickerView3;
self.textField4.inputView = self.pickerView4;
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0 {
return hazards.count
} else if pickerView.tag == 1 {
return reasons.count
} else if pickerView.tag == 2 {
return site.count
} else if pickerView.tag == 3 {
return line.count
}
return 1
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if pickerView.tag == 0 {
return hazards[row]
} else if pickerView.tag == 1 {
return reasons[row]
} else if pickerView.tag == 2 {
return site[row]
} else if pickerView.tag == 3 {
return line[row]
}
return ""
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
textField1.text = hazards[row]
} else if pickerView.tag == 1 {
textField2.text = reasons[row]
} else if pickerView.tag == 2 {
textField3.text = site[row]
} else if pickerView.tag == 3 {
textField4.text = line[row]
}
}
}
答案 0 :(得分:10)
您正在使用选择器视图'tag
属性来确定哪个数组是给定选取器视图的数据源,但您最初没有设置标记。 tag
默认为零,因此所有四个选择器视图都显示相同的数据。在实例化选择器视图后,添加以下内容:
pickerView1.tag = 0
pickerView2.tag = 1
pickerView3.tag = 2
pickerView4.tag = 3