Multiple Custom UIPickerViews in the same UIView in swift - ios

Im trying to have two different pickerViews on the same viewController and let them represent different data. I have tried a different ways to do this, but both picker views showing same Data, but can't put it all together. Any help would be awesome!
here is my code:
#IBOutlet weak var Eventtype: UITextField!
let pickerView = UIPickerView()
var Eventstype = ["Birthday" , "Marriege" , "Get together", "Conference"]
#IBOutlet weak var datepickertxt: UITextField!
var datepicker = UIDatePicker()
#IBOutlet weak var Duration: UITextField!
let duration = UIPickerView()
var Durations = ["6-8","1-5","7-9","10-12"]
override func viewDidLoad() {
super.viewDidLoad()
createDatePicker()
pickerView.delegate = self
pickerView.dataSource = self
duration.delegate = self
duration.dataSource = self
Eventtype.inputView = pickerView
Duration.inputView = duration
// Do any additional setup after loading the view.
}
////For Event Picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 0) {
//pickerView1
return Eventstype.count
} else if (duration.tag == 1){
//pickerView2
return Durations.count
}
return 1
//return DurationTxt.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 0) {
return Eventstype[row]
} else if (duration.tag == 1){
return Durations[row]
}
return ""
}
//return DurationTxt[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView.tag == 0) {
Eventtype.text = Eventstype[row]
self.view.endEditing(false)
}else if (duration.tag == 1){
Duration.text = Durations[row]
self.view.endEditing(true)
}
}

#IBOutlet weak var Eventtype: UITextField!
let pickerView = UIPickerView()
var Eventstype = ["Birthday" , "Marriege" , "Get together", "Conference"]
#IBOutlet weak var datepickertxt: UITextField!
var datepicker = UIDatePicker()
#IBOutlet weak var Duration: UITextField!
let duration = UIPickerView()
var Durations = ["6-8","1-5","7-9","10-12"]
override func viewDidLoad() {
super.viewDidLoad()
createDatePicker()
pickerView.delegate = self
pickerView.dataSource = self
duration.delegate = self
duration.dataSource = self
Eventtype.inputView = pickerView
Duration.inputView = duration
pickerView.tag = 0
duration.tag = 1
// Do any additional setup after loading the view.
}
////For Event Picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.tag == 0) {
//pickerView1
return Eventstype.count
} else if (duration.tag == 1){
//pickerView2
return Durations.count
}
return 1
//return DurationTxt.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView.tag == 0) {
return Eventstype[row]
} else if (duration.tag == 1){
return Durations[row]
}
return ""
}
//return DurationTxt[row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView.tag == 0) {
Eventtype.text = Eventstype[row]
self.view.endEditing(false)
}else if (duration.tag == 1){
Duration.text = Durations[row]
self.view.endEditing(true)
}
}
This is done by tag or else yo can check in delegate like
if pickerView == duration

Set the tags for pickerview in viewDidLoad as shown below,
pickerView.tag = 0
duration.tag = 1

class ViewController: UIViewController {
#IBOutlet var txtDuration: UITextField!
#IBOutlet var txtEventType: UITextField!
var pickerView = UIPickerView()
var arrEventType : [String] = []
var arrDuration : [String] = []
var currentTextField : UITextField?
override func viewDidLoad() {
super.viewDidLoad()
arrEventType = ["Birthday" , "Marriage" , "Get together", "Conference"]
arrDuration = ["6-8","1-5","7-9","10-12"]
}
}
extension ViewController : UIPickerViewDelegate , UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if currentTextField == txtEventType {
return arrEventType.count
} else if currentTextField == txtDuration {
return arrDuration.count
} else {
return 0
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if currentTextField == txtEventType {
return arrEventType[row]
} else if currentTextField == txtDuration {
return arrDuration[row]
} else {
return ""
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if currentTextField == txtEventType {
txtEventType.text = arrEventType[row]
} else if currentTextField == txtDuration {
txtDuration.text = arrDuration[row]
}
}
}
extension ViewController : UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
self.pickerView.delegate = self
self.pickerView.dataSource = self
currentTextField = textField
if currentTextField == txtEventType {
currentTextField?.inputView = pickerView
} else if currentTextField == txtDuration {
currentTextField?.inputView = pickerView
}
}
}
Above lines of code may solve your issue with just one UIPickerView and without tags

Related

3 text fields, 1 pickerview, but, when using pickerView, the selection does not show in text field

UIPickerView shows data but does not transfer to UITextField.
i've checked other post to compare my code, still cannot find a solution. your help is greatly appreciated.
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
#IBOutlet weak var hospNameField: UITextField!
#IBOutlet weak var teamNameField: UITextField!
#IBOutlet weak var mdNameField: UITextField!
#IBOutlet weak var selectionsPickerView: UIPickerView!
var hosp = ["hosp1", "hosp2", "hosp3"]
var team = ["team1", "team2", "team3"]
var mds = ["MD1", "MD2", "MD3"]
var itemSelected = ""
var textFieldSelected = UITextField()
weak var pickerView: UIPickerView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
hospNameField.inputView = pickerView
teamNameField.inputView = pickerView
mdNameField.inputView = pickerView
hospNameField.delegate = self
teamNameField.delegate = self
mdNameField.delegate = self
self.pickerView = pickerView
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldSelected = textField
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if hospNameField.isFirstResponder {
return hosp.count
} else if teamNameField.isFirstResponder {
return team.count
} else if mdNameField.isFirstResponder {
return mds.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if hospNameField.isFirstResponder {
return hosp[row]
} else if teamNameField.isFirstResponder {
return team[row]
} else if mdNameField.isFirstResponder {
return mds[row]
}
return nil
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if teamNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
}
}
You have forgotten to subscribe the delegate and data sources to the PickerView..
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
#IBOutlet weak var hospNameField: UITextField!
#IBOutlet weak var teamNameField: UITextField!
#IBOutlet weak var mdNameField: UITextField!
#IBOutlet weak var selectionsPickerView: UIPickerView!
var hosp = ["hosp1", "hosp2", "hosp3"]
var team = ["team1", "team2", "team3"]
var mds = ["MD1", "MD2", "MD3"]
var itemSelected = ""
var textFieldSelected = UITextField()
weak var pickerView: UIPickerView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let pickerView = UIPickerView()
pickerView.delegate = self
pickerView.dataSource = self
hospNameField.inputView = pickerView
teamNameField.inputView = pickerView
mdNameField.inputView = pickerView
hospNameField.delegate = self
teamNameField.delegate = self
mdNameField.delegate = self
self.pickerView = pickerView
/**
the below lines are mandatory for
pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
to execute
*/
self.pickerView?.delegate = self
self.pickerView?.dataSource = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func textFieldDidBeginEditing(_ textField: UITextField) {
textFieldSelected = textField
}
// add this delegate and reload here
func textFieldShouldBeginEditing(_ textField: UITextField) ->
Bool {
self.pickerView?.reloadAllComponents()
return true
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if hospNameField.isFirstResponder {
return hosp.count
} else if teamNameField.isFirstResponder {
return team.count
} else if mdNameField.isFirstResponder {
return mds.count
}
return 0
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if hospNameField.isFirstResponder {
return hosp[row]
} else if teamNameField.isFirstResponder {
return team[row]
} else if mdNameField.isFirstResponder {
return mds[row]
}
return nil
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
/**
type here, need to check the responder for hospNameField also
you have executed teamNameField.isFirstResponder twice
*/
if hospNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
}
}
I think your code is wrong in didSelectRow. check the first and second if, both run the same condition.
if teamNameField.isFirstResponder {
let itemselected = hosp[row]
hospNameField.text = itemselected
} else if teamNameField.isFirstResponder {
let itemselected = team[row]
teamNameField.text = itemselected
} else if mdNameField.isFirstResponder {
let itemselected = mds[row]
mdNameField.text = itemselected
}
I think you should change the first if to if hospNameField.isFirstResponder {..}

swift 4 multiple picker view not working. I have three arrays ,but only "strBlood" is showing

swift 4 multiple picker view not working. I have three arrays ,but only "strBlood" is showing the accurate array , other strcountry and strgender are not showing in the picker view and when I click on to the third label code crashes giving me the error of signal sigbart
multiple picker views are not showing the data in the picker view in swift 4, I already read many tutorials but no one solve my problem please check and answer me.
when I implemented 3 picker views the data is showing only of the first picker view i.e , (strblood) but not the other arrays
the code below is working properly but there is one error regarding showing the array in the picker view when clciked on the other labels ("lblblood"."lblcountry")
import UIKit
import Foundation
class RegisterViewController: UIViewController {
var strBlood = ["O+","O-","O","A","B+"]
var strcountry = ["India","Canada","USA"]
var strgender = ["Male","Female"]
var selectedBlood: String?
var selectedCountry: String?
var selectedGender: String?
#IBOutlet weak var txtGender: UITextField!
#IBOutlet weak var txtCountry: UITextField!
#IBOutlet weak var lblBloodGroup: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.chooseCountry()
self.chooseBlood()
self.choosGender()
}
func chooseBlood(){
let bloodPicker = UIPickerView()
bloodPicker.delegate = self
self.lblBloodGroup.inputView = bloodPicker
}
func chooseCountry(){
let countryname = UIPickerView()
countryname.delegate = self
self.txtCountry.inputView = countryname
}
func choosGender() {
let gender1 = UIPickerView()
gender1.delegate = self
self.txtGender.inputView = gender1
}
}
extension RegisterViewController : UIPickerViewDelegate , UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if lblBloodGroup.isEnabled {
print("BLOOD SELECTED")
return strBlood.count
}
else if txtCountry.isEnabled{
print("COUNTRY SELECTED")
return strcountry.count
}else {
print("GENDER SELECTED")
return strgender.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if lblBloodGroup.isEnabled{
print("BLOOD SELECTED1")
return strBlood[row]
}else if txtCountry.isEnabled{
return strcountry[row]
}else {
return strgender[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if lblBloodGroup.isEnabled{
selectedBlood = strBlood[row]
lblBloodGroup.text = selectedBlood
}else if txtCountry.isEnabled{
selectedCountry = strcountry[row]
txtCountry.text = selectedCountry
}else {
selectedGender = strgender[row]
txtGender.text = selectedGender
}
}
}
To achieve this isEnabled will not help. you need to use isEditing and it will be true if user click on any UITextField and your code will be:
import UIKit
import Foundation
class ViewController: UIViewController {
var strBlood = ["O+","O-","O","A","B+"]
var strcountry = ["India","Canada","USA"]
var strgender = ["Male","Female"]
var selectedBlood: String?
var selectedCountry: String?
var selectedGender: String?
#IBOutlet weak var txtGender: UITextField!
#IBOutlet weak var txtCountry: UITextField!
#IBOutlet weak var lblBloodGroup: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.chooseCountry()
self.chooseBlood()
self.choosGender()
}
func chooseBlood(){
let bloodPicker = UIPickerView()
bloodPicker.delegate = self
self.lblBloodGroup.inputView = bloodPicker
}
func chooseCountry(){
let countryname = UIPickerView()
countryname.delegate = self
self.txtCountry.inputView = countryname
}
func choosGender() {
let gender1 = UIPickerView()
gender1.delegate = self
self.txtGender.inputView = gender1
}
}
extension ViewController : UIPickerViewDelegate , UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if lblBloodGroup.isEditing {
print("BLOOD SELECTED")
return strBlood.count
}
else if txtCountry.isEditing{
print("COUNTRY SELECTED")
return strcountry.count
}else {
print("GENDER SELECTED")
return strgender.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if lblBloodGroup.isEditing{
print("BLOOD SELECTED1")
return strBlood[row]
}else if txtCountry.isEditing{
return strcountry[row]
}else {
return strgender[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if lblBloodGroup.isEditing{
selectedBlood = strBlood[row]
lblBloodGroup.text = selectedBlood
}else if txtCountry.isEditing{
selectedCountry = strcountry[row]
txtCountry.text = selectedCountry
}else {
selectedGender = strgender[row]
txtGender.text = selectedGender
}
}
}
And result will be:

Using a picker view based on what has been selected before

I looked on here for an answer, but do not believe this question has been answered. I have multiple text boxes that use a couple different pickers. I would like for the first text box the user selects the material, and then based on the material selected they can only see the type of material that corresponds to it. Below in my code when I am naming my arrays you can see that it says typeBrassData or typeCopperData so if the user selects Brass or Copper above only the options for Brass show, does anyone know how to do this? Below is the code I was using when it was only 2 picker views. Thanks all in advance!
import UIKit
class MetalCalculatorViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var materialField: UITextField!
#IBOutlet weak var typeField: UITextField!
#IBOutlet weak var volumeField: UITextField!
#IBOutlet weak var quantityField: UITextField!
var pickerMaterial = UIPickerView()
var pickerType = UIPickerView()
var materialData = ["Brass","Copper","CuNiSn", "Cupro-Nickel","High Performance Alloys", "Leaded Brass","Nickel Silvers","Phosphor Bronze","Tin Brass"]
var typeData = ["C101", "C102","C1092","C110","C122","C14415","C151", "C155","C18070", "C18080", "C19020", "C19025", "C19210","C194", "C195", "C197", "C1972", "C210","C220", "C226", "C230", "C240", "C260","C268","C272", "C350", "C353", "C422", "C425", "C4252", "C510","C511", "C5118", "C519","C521", "C638", "C654", "655", "C688", "C7025", "C70250", "C7026", "C7035", "C706", "C710", "C715", "C7250", "C752", "C757","C764","C770", "XP5", "XP10", "XP55", "XP125", "XP150", "XP175", "MAX251C", "MSP1"]
var typeBrassData = ["C210","C220", "C226", "C230", "C240", "C260","C268","C272"]
var typeHighPerformanceData = ["C14415","C151", "C155","C18070", "C18080", "C19020", "C19025", "C19210","C194", "C195", "C197", "C1972", "C638", "C654", "655", "C688", "C7025", "C70250", "C7026", "C7035", "XP5", "XP10", "XP55", "XP125", "XP150", "XP175", "MAX251C", "MSP1"]
var typeCopperData = ["C101", "C102","C1092","C110","C122"]
var typeCuproNickelData = ["C706", "C710", "C715"]
var typeLeadedBrassData = ["C350", "C353"]
var typeTinBrassData = ["C422", "C425", "C4252"]
var typePhosphorBronzeData = ["C510","C511", "C5118", "C519","C521"]
var typeCunNiSnData = ["C725"]
var typeNickelSilverData = ["C752", "C757","C764","C770"]
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidLoad() {
super.viewDidLoad()
pickerMaterial.delegate = self
pickerMaterial.dataSource = self
pickerMaterial.tag = 1
materialField.inputView = pickerMaterial
pickerType.delegate = self
pickerType.dataSource = self
pickerType.tag = 2
typeField.inputView = pickerType
let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(MetalCalculatorViewController.dismissKeyboard))
view.addGestureRecognizer(tap)
// Do any additional setup after loading the view.
}
func dismissKeyboard() {
view.endEditing(true)
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == pickerMaterial {
return materialData.count
} else if pickerView == pickerType{
return typeData.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pickerMaterial {
return materialData[row]
} else if pickerView == pickerType{
return typeData[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pickerMaterial {
materialField.text = materialData[row]
} else if pickerView == pickerType{
typeField.text = typeData[row]
}
}
You can use the dictionary data structure to set the values of the pickerviews
var materialSelected = ""
var typeArr = [String]()
//I have done for only three materials obviously you can extend it...
var dict = ["Brass" : ["C210","C220", "C226", "C230", "C240", "C260","C268","C272"],
"Copper" : ["C101", "C102","C1092","C110","C122"], "CuNiSn" : ["C725"]] as [String: Any]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == pickerMaterial {
return materialData.count
} else if pickerView == pickerType{
typeArr = dict[materialSelected] as! [String]
return typeArr.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pickerMaterial {
return materialData[row]
} else if pickerView == pickerType{
typeArr = dict[materialSelected] as! [String]
return typeArr[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pickerMaterial {
materialSelected = materialData[row]
materialField.text = materialSelected
} else if pickerView == pickerType{
typeArr = dict[materialSelected] as! [String]
typeField.text = typeArr[row]
}
}

Pickerview not displaying

My pickerview is not displaying for some reason even after reloading all components. I click start and it just does not load up the pickerview as I want this to load and then you can select an option and go to the next question?
import UIKit
class QuestionsViewController: UIViewController, UIPickerViewDelegate {
#IBOutlet weak var Next: UIButton!
#IBOutlet weak var pickerview: UIPickerView!
#IBOutlet weak var itemLabel: UILabel!
#IBOutlet weak var label1: UILabel!
#IBOutlet weak var Question: UILabel!
#IBAction func Next(_ sender: Any) {
cQuestion.currentQuestion = cQuestion.currentQuestion + 1
pickerview.reloadAllComponents()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//var LabelText: String = ""
//var arrayOfQuestions: [String] = ["&","&&","||","None of above"]
//var correctAns:String = ""
//var currentQuestion = 0
let cQuestion = Questions()
//init(arrayOfQuestions:String, correctAns:String, LabelText:String) {
//self.arrayOfQuestions = [arrayOfQuestions]
// self.correctAns = correctAns
// self.LabelText = LabelText
//}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
itemLabel.text = cQuestion.arrayOfQuestions[0]
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if(cQuestion.currentQuestion == 0) {
Question.text = "Q1. Which is a logical OR operator"
return cQuestion.arrayOfQuestions.count
} else if (cQuestion.currentQuestion == 1) {
Question.text = "Q2. Compiler generates_file"
return cQuestion.arrayOfQuestions.count
}
hide()
Question.text = "You have finished"
Next.isHidden = true
return cQuestion.arrayOfQuestions.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
if(cQuestion.currentQuestion == 0) {
return cQuestion.arrayOfQuestions[row]
} else if (cQuestion.currentQuestion == 1) {
return cQuestion.arrayOfQuestions[row]
}
return cQuestion.arrayOfQuestions[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
if (cQuestion.currentQuestion == 0) {
let itemSelected = cQuestion.arrayOfQuestions[row]
itemLabel.text = itemSelected
} else if (cQuestion.currentQuestion == 1) {
let itemSelected = cQuestion.arrayOfQuestions[row]
itemLabel.text = itemSelected
}
}
func hide() {
pickerview.isHidden = true
itemLabel.isHidden = true
}
}
My guess: it looks like as soon as numberOfRowsInComponent is called (which it does as soon as it prepares to be added to the view) you also call hide() which in turns seems to set your pickerView to isHidden = true. I don't see anywhere in your code where isHidden is subsequently set to false.
If that happens to not be the case, also check what the value of pickerview.dataSource is to make sure it's not nil.

How to create multiple pickerviews programmatically

I am trying to create a separate pickerview for each textbox that is selected. so far the closest I have gotten is having the same pickerview for each of the text boxes that need their own separate list of items to choose from.
class MainViewController: UIViewController, UITextFieldDelegate, UIPickerViewDataSource, UIPickerViewDelegate {
var dogBreedPickerArray = ["German Shepherd", "Huskie", "cray cray"]
var catBreedPickerArray = ["savanna", "cheetoh", "bengal"]
var maleFemalePickerArray = ["Male", "Female"]
var dogBreedPicker = UIPickerView()
var dogBreedPicker = UIPickerView()
var catBreedPicker = UIPickerView()
var maleFemalePicker = UIPickerView()
#IBOutlet var dogBreedTextBox: UITextField!
#IBOutlet var catBreedTextBox: UITextField!
#IBOutlet var maleFemaleTextBox: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
dogBreedPicker.delegate = self
dogBreedPicker.dataSource = self
dogBreedTextBox.inputView = dogBreedPicker
dogBreedTextBox.delegate = self
}
// dog picker functions
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dogBreedPickerArray.count
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
petBreedTextBox.text = dogBreedPickerArray[row]
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dogBreedPickerArray[row]
}
}
its just a example may be it help to you
after selecting any text field asign textfield tag to picker and reload the component.
// variable
var heightInch:[Int] = [Int]()
var heightfeet:[Int] = [Int]()
var heightCms:[Int] = [Int]()
delegate methods
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
if picker.tag == 1{
return 2
}else{
return 1
}
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if picker.tag == 1{
switch component {
case 0:
return heightfeet.count
default:
return heightInch.count
}
}else{
return heightCms.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if picker.tag == 1{
switch component {
case 0:
return String(heightfeet[row])
default:
return String(heightInch[row])
}
}else{
return String(heightCms[row])
}

Resources