同じビュー内にPickerViewを二つ作成して、
それぞれ違う値を格納したいのですが、
下記コードで実行すると、viewDidLoadでpickerView1と
pickerView2それぞれのaへの代入処理がされてしまい
結果的に両方のpickerView共にarray2の値が表示されてしまいます。
pickerView1にはarray1,pickerView2にはarray2の値を
表示したいのですがどのような処理に変更すれば解決できるでしょうか。
回答の程、よろしくおねがいいたします。
@IBOutlet weak var textField1: UITextField! @IBOutlet weak var textField2: UITextField! var pickerView1: UIPickerView = UIPickerView() var array1 = ["1", "2", "3"] var pickerView2: UIPickerView = UIPickerView() var array2 = ["A", "B", "C"] var a = 0 override func viewDidLoad() { super.viewDidLoad() //pickerView1 pickerView1.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: pickerView1.bounds.size.height) pickerView1.delegate = self pickerView1.dataSource = self let vi1 = UIView(frame: pickerView1.bounds) vi1.backgroundColor = UIColor.white vi1.addSubview(pickerView1) textField1.inputView = vi1 let toolBar1 = UIToolbar() toolBar1.barStyle = UIBarStyle.default toolBar1.isTranslucent = true toolBar1.tintColor = UIColor.black let doneButton1 = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(ViewController.donePressed)) let spaceButton1 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) toolBar1.setItems([spaceButton1, doneButton1], animated: false) toolBar1.isUserInteractionEnabled = true toolBar1.sizeToFit() textField1.inputAccessoryView = toolBar1 a = 1 //pickerView2 pickerView2.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: pickerView2.bounds.size.height) pickerView2.delegate = self pickerView2.dataSource = self let vi2 = UIView(frame: pickerView2.bounds) vi2.backgroundColor = UIColor.white vi2.addSubview(pickerView2) textField2.inputView = vi2 let toolBar2 = UIToolbar() toolBar2.barStyle = UIBarStyle.default toolBar2.isTranslucent = true toolBar2.tintColor = UIColor.black let doneButton2 = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(ViewController.donePressed)) let spaceButton2 = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil) toolBar2.setItems([spaceButton2, doneButton2], animated: false) toolBar2.isUserInteractionEnabled = true toolBar2.sizeToFit() textField2.inputAccessoryView = toolBar2 a = 2 } // Done func donePressed() { view.endEditing(true) } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { if a = 1{ return array1[row] } else { return array2[row] } } func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { if a = 1 { return array1.count } else { return array2.count } } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { if a = 1 { textField1.text = array1[row] ] else { textField2.text = array2[row] } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/12/25 12:23
2018/09/21 03:59
2018/09/21 04:33
2018/09/21 06:29
2018/09/21 06:34
2018/09/21 06:40
2018/09/21 06:41