1

So Im trying to use two separate pickers in one single view. The second set of data will update based on the choice in the first view picker. My issue is that every time I select something in the second view picker it changes the selection in the first one. How do I keep the data in the first from changing when I make a selection in the second row.

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{


    @IBOutlet weak var see: UITextField!


    var pick = ["1", "2", "3", "4"]
    var picker = UIPickerView()
    var picker2 = UIPickerView()


    @IBOutlet weak var see2: UITextField!


    var viewone = ["1", "2", "3"]
    var viewtwo = ["1", "2", "3"]


    override func viewDidLoad() {
        super.viewDidLoad()
        picker.delegate = self
        picker.dataSource = self
        picker2.delegate = self
        picker2.dataSource = self
        see.inputView = picker
        see2.inputView = picker2
        picker.tag = 1
        picker2.tag = 2
    }

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView.tag == 1 {
            return pick.count
        }else if pickerView.tag == 2{
            if see.text == "1"{
                return viewone.count
            }else if see.text == "2"{
                return viewtwo.count
            }else{
                return 1
            }
        }else{
            return 1
        }
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        see.text = pick[row]

    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView.tag == 1{
            return "\(pick[row])"
        }else if pickerView.tag == 2{
            if see.text == "1"{
                return "\(viewone[row])"
            }else if see.text == "2"{
                return "\(viewtwo[row])"
            }else{
                return nil
            }
        }else{
            return nil
        }
    }

2 Answers 2

1

You need to change the text only if the selected row is in the second pickerview

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
   if(pickerView == picker2){ 
      see.text = pick[row]
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

The delegate and datasource methods are the same for all picker views. The difference is in the pickerView argument which is passed to the method.

If you're just talking about keeping the pickers and data separate then you can do something like this.

if (pickerView == self.picker) {
    return _pickerData.count;
} else if (pickerView == self.picker2) {
    return _pickerData2.count;
} else if (pickerView == self.picker3) {
    return _pickerData3.count;
}

1 Comment

Im want the data values for picker2 to change depending on the selection in picker . I can get the to populate properly but when i am using the selector in picker2 it will change my selection in picker.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.