Search code examples
iosswiftuitextfielduipickerview

How can I change the strings of a UIPickerView from another UIPickerview?


I have two UIPickerview, pickerView1 and pickerView2 in my view controller.

What I want to achieve here is when I select a string from my pickerView1, it automatically changes the strings for the pickerView2.

I created a section 3 because there was a lot of strings, ideally I want this all in section 1, as I currently have my code set to 3 picker views but I only want two picker views.

Here is an example.

Pickerview1 contains "a","b","c","d".

If "a" is selected, the pickerView2 would show specific strings for that to choose from, if I choose "b" then the strings for "a" would not show but the strings for "b" would show separately.

In section 2 I want each of those fields to correspond with the sub strings that are defined in section 1 and section 3, this then populates the second UIpickerview, so every time i select a string in the first picker view the second uipickerview generates the specific strings for that.

Here is my code:

//PickerView Category IBOutlets

    @IBOutlet var pickerView1: UIPickerView! = UIPickerView()
    @IBOutlet var pickerView2: UIPickerView! = UIPickerView()
    @IBOutlet var pickerView3: UIPickerView! = UIPickerView()
    @IBOutlet var textField1: UITextField! = UITextField()
    @IBOutlet var textField2: UITextField! = UITextField()
    @IBOutlet var textField3: UITextField! = UITextField()

    var SECTION2 = ["Select", "Criminal Activity", "Begging","Harassment", "Criminal Damage", "Disruption Of Service", "Drunken Behaviour","Fare Evasion", "Feet on Seats", "Hate Crime", "Inappropriate Sexual Behaviour", "Litter", "Missile Throwing", "Noise","Rowdy Behaviour", "Smoking Tobacco", "Drugs", "Threats/abuse/intimidation towards Staff", "Threats/abuse/intimdation towards Passengers" ]

    var SECTION1 = ["Select", "*>>>Criminal Activity<<<*", "Assault", "Arson", "Trespass", "Spitting", "*>>>Begging<<<*", "On Board", "Off Board", "*>>>Harassment<<<*", "Stalking", "On Board", "At Stop/Station", "Bullying", "*>>>Criminal Damage<<<*", "Damage to Exterior", "Damage to Interior", "Glass Breakage","Grafitti", "Window Etching", "*>>>Disruption Of Service<<<*", "Interfering with the engine", "Opening emergency exits", "Pressing emergency stop/handles", "Throwing debris on to the line/track", "*>>>Drunken Behaviour<<<*", "Drinking on the mode of transport", "Drinking in the station/stop", "Aggressive Drunks", "*>>>Fare Evasion<<<*", "Fraudulent Pass", "None payment of fare", "*>>>Feet on Seats<<<*", "Purposeful Obstruction" ]

    var SECTION3 = ["Select", "*>>>Hate Crime<<<*", "Disability", "Ethnicity", "Gender", "Race","Religion", "Sexual Orientation", "*>>>Litter<<<*", "On Board", "At station/stop", "*>>>Missile Throwing<<<*", "Stone Throwing", "Throwing Objects", "*>>>Noise<<<*", "Inappropriate Musical Content", "Loud Music", "*>>>Rowdy Behaviour<<<*", "School Children", "Shouting/Swearing", "Within Shelter", "Within Station/Stop"," Groups/Gangs", "*>>>Smoking Tobacco<<<*", "On Board", "At station/stop", "*>>>Drugs<<<*", "Smoking Drugs", "Drug Paraphernalia", "Drug Taking", "Drug Dealing", "*>>>Threats/abuse/intimidation towards Staff<<<*", "On Board", "At station/stop", "*>>>Threats/abuse/intimidation towards Passengers<<<*", "On Board", "At station/stop" ]

    override func viewDidLoad() {
        super.viewDidLoad()

        var Viewcontroller: () =
        pickerView1 = UIPickerView()
        pickerView2 = UIPickerView()
        pickerView3 = UIPickerView()
        pickerView1.delegate = self
        pickerView2.delegate = self
        pickerView3.delegate = self

        self.textField1.inputView = self.pickerView1;
        self.textField2.inputView = self.pickerView2;
        self.textField3.inputView = self.pickerView3;

        pickerView1.tag = 0
        pickerView2.tag = 1
        pickerView3.tag = 2

        textField1.inputView = pickerView1
        textField2.inputView = pickerView2
        textField3.inputView = pickerView3
    }

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

    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if pickerView.tag == 0 {
            return SECTION1.count
        } else if pickerView.tag == 1 {
            return SECTION2.count
        } else if pickerView.tag == 2 {
            return SECTION3.count
        }
        return 1
    }

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

        if pickerView.tag == 0 {
            return SECTION1[row]
        } else if pickerView.tag == 1 {
            return SECTION2[row]
        } else if pickerView.tag == 2 {
            return SECTION3[row]
        }
        return ""
    }

    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)  {

        if pickerView.tag == 0 {
            textField1.text = SECTION1[row]
        } else if pickerView.tag == 1 {
            textField2.text = SECTION2[row]
        } else if pickerView.tag == 2 {
            textField3.text = SECTION3[row]
        }

    }
}

Solution

  • set the global variables

    var data1:NSMutableArray=["Apple","Mango","Banana"]
        var data2:NSMutableArray=[["greenApple","whiteApple","yellowApple"],["greenMango","whiteMango","yelloMango"],["greenBanana","whiteBanana","yellowBanana"]]
        var data3:NSArray = []
    

    Set the tags for pickerViews - pickerView1 (say 1) and pickerView2 (say 2).

         override func viewDidLoad() {
                super.viewDidLoad()
          pickerView1.tag=1;
                pickerView2.tag=2;
    data3=["greenApple","whiteApple","yellowApple"] //initialise the array with the data
         }
    

    set the title in delegate method

    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        if pickerView.tag==1 {
        return data1[row] as! String
        }
        else {
            return data3[row] as! String
        }
    
    }
    

    In selection delegate if the picker one is selected then assign the data3 with the required data based on pickerView1 selection and reload the pickerView2

     func pickerView(pickerView: UIPickerView,
        didSelectRow row: Int,
        inComponent component: Int)
    {
    
         if pickerView.tag==1 {
            data3=data2[row] as! NSArray
            pickerView2.reloadAllComponents() // reload pickerview2
        }
    
    }