user3700449 user3700449 - 5 months ago 130
Swift Question

How to set data for multiple UIpickerviews in Swift?

I have

uiviewcontroller
with
uitextfields
, when users tap on the textfields
uipickerview
appears as shown in the attachment.

How can I make multiple
uipickerview
? For example, when the user tap on the countries textfields a pickerview shows up with countries array data and when users tap on the gender textfields uipickerview shows the genders array data and so on. I tried but only able to do it for one array which is countries as shown in my code. would appreciate if you could show me how to do it for the rest of my uitextfields.

import UIKit

class testVC: UIViewController,UITextViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,UIPickerViewDelegate,UIPickerViewDataSource {

var countries = ["USA","UK","Spain" ]
var gender = ["Male","Female","Both"]
var language = ["English","French","Spanish","Other"]

override func viewDidLoad() {
super.viewDidLoad()
var pickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 100))

pickerView.backgroundColor = .whiteColor()

pickerView.showsSelectionIndicator = true

var toolBar = UIToolbar()
toolBar.barStyle = UIBarStyle.Default
toolBar.translucent = true
toolBar.tintColor = UIColor(red: 76/255, green: 217/255, blue: 100/255, alpha: 1)
toolBar.sizeToFit()

pickerView.delegate = self
pickerView.dataSource = self

[![enter image description here][1]][1]
var doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Plain, target: self, action: "donePicker")
var spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
var cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: "canclePicker")

toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
toolBar.userInteractionEnabled = true


genderTxt.inputView = pickerView
genderTxt.inputAccessoryView = toolBar

countriesTxt.inputView = pickerView
countriesTxt.inputAccessoryView = toolBar

categoriesTxt.inputView = pickerView
categoriesTxt.inputAccessoryView = toolBar

startDateTxt.inputView = pickerView
startDateTxt.inputAccessoryView = toolBar

endDateTxt.inputView = pickerView
endDateTxt.inputAccessoryView = toolBar

languageTxt.inputView = pickerView
languageTxt.inputAccessoryView = toolBar

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {

return 1

}


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

return countries.count

}

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


return countries[row]
}

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

var itemselected = countries[row]
countriesTxt.text = itemselected

}

Answer

You don't need multiple UIPickerView. Instead you need to set picker view data source based on first responder text view. Also you should implement textFieldShouldBeginEditing delegate method to reload content of picker view by calling its reloadAllComponents method

var pickerView : UIPickerView!  

override func viewDidLoad() {

     self.pickerView = UIPickerView(frame: CGRectMake(0, 200, view.frame.width, 100))
     NSNotificationCenter.defaultCenter().addObserver(self, selector: ("updatePicker"), name: UITextFieldTextDidBeginEditingNotification, object: nil)
}

func updatePicker(){
   self.pickerView.reloadAllComponents()
}

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

        if countriesTxt.isFirstResponder(){
             return countries.count
        }else if genderTxt.isFirstResponder(){
             return gender.count
        }
        // continue this way and implement all cases

}

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

        if countriesTxt.isFirstResponder(){
              return countries[row]
        }else if genderTxt.isFirstResponder(){
              return gender[row]

        }
        // continue this way and implement all cases

}

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

        if countriesTxt.isFirstResponder(){
              var itemselected = countries[row]
              countriesTxt.text = itemselected
        }else if genderTxt.isFirstResponder(){
               var itemselected = gender[row]
              genderTxt.text = itemselected
        } 
        // ...


}