How to create multiple pickerviews programmatically - ios

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])
}

Related

2 TextField with 2 picker in 1 UIView

I made a 2 textField with 2 pickers in 1UIView but I can't show the right pickers in the right textfield because I don't know how recognize the current view active.
I tried something with tag (but only work with pickers, no with textfield), and create var current = UITextField()
var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var picker = UIPickerView()
var currentTextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
GenderTextField.inputView = picker
WitchSportTextField.inputView = picker
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if currentTextField == GenderTextField{
return genre.count
}else{
return Sport.count
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if currentTextField == GenderTextField {
GenderTextField.text = genre[row]
}else{
WitchSportTextField.text = Sport[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if currentTextField == GenderTextField{
return genre[row]
}else{
return Sport[row]
}
}
Someone as an idea ?
here is the complete code
var genre = ["-","Homme","Femme"]
var Sport = ["-","Tennis"]
var genrepicker = UIPickerView()
var Sportpicker = UIPickerView()
var genretTextField = UITextField()
var SportTextField = UITextField()
override func viewDidLoad() {
super.viewDidLoad()
genrepicker.delegate = self
genrepicker.dataSource = self
Sportpicker.delegate = self
Sportpicker.dataSource = self
genretTextField.inputView = genrepicker
SportTextField.inputView = Sportpicker
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == genrepicker{
return genre.count
}else{
return Sport.count
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == genrepicker {
GenderTextField.text = genre[row]
}else{
WitchSportTextField.text = Sport[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == genrepicker{
return genre[row]
}else{
return Sport[row]
}
}
Firstly assign your both TextField delegate to self in viewDidLoad
GenderTextField.delegate = self
WitchSportTextField.delegate = self
Use this extension for identified which TextField is active.
extension YourClass: UITextFieldDelegate {
func textFieldDidBeginEditing(_ textField: UITextField) {
currentTextField = textField
picker.reloadAllComponents() //Reload for accurate data
}
}
NOTE: Variable names should begin with lower case letter. Use descriptive names to clarify your intent:
Look at this: https://github.com/MLSDev/development-standards/blob/master/platform/ios/swift-coding-conventions.md

UIPICKERVIEW with different data to be displayed according to the row selected

I have attached my code for your reference. I can able to display the data in Pickerview as i change the row the data changes accordingly but the data which selected from the picker is not getting displayed in the UITextfield. if I select any option in Pickerview I could not see any text inside the uitextfield if I select it still showing blank text field.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
#IBOutlet weak var textfield1: UITextField!
#IBOutlet weak var textfield2: UITextField!
var x = 0
let leaguepickerview_destination = UIPickerView()
let clubpickerview_denomination = UIPickerView()
let league_destination = ["India","Europe","Australia","New Zealand","USA & Rest of the World"]
let club_denomination = [[" Rs.500","Rs.1000","Rs.1500","Rs.2000","Rs.2500","Rs.3000","Rs.3500","Rs.4000","Rs.4500","Rs.5000"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"],["100","150","200","250","300","350","400","450","500"]]
override func viewDidLoad() {
super.viewDidLoad()
leaguepickerview_destination.delegate = self
leaguepickerview_destination.dataSource = self
clubpickerview_denomination.delegate = self
clubpickerview_denomination.dataSource = self
textfield1.inputView = leaguepickerview_destination
textfield2.inputView = clubpickerview_denomination
textfield1.delegate = self
textfield2.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
switch (pickerView){
case leaguepickerview_destination:
return league_destination.count
case clubpickerview_denomination:
return club_denomination[x].count
default:
return 0
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch (pickerView){
case leaguepickerview_destination:
return league_destination[row]
case clubpickerview_denomination:
return club_denomination[x][row]
default:
return "an error occurred"
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == leaguepickerview_destination) {
x = row
clubpickerview_denomination.reloadAllComponents()
}
}
}
You need to set the textfield text
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == leaguepickerview_destination) {
x = row
clubpickerview_denomination.reloadAllComponents()
textfield1.text = league_destination[row]
}
else {
textfield2.text = club_denomination[x][row]
}
}

How to use UIPickerView with TextField more than once?

Below is my code, this allows one Textfield to have a pop up pickerView as an input source. I want to know what i need to add to allow a second TextField to also have a pop up pickerView as a input source.
I have managed to get the firstNametextfield working, but havent managed to get the secondNames working.
class RefereeViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate, UINavigationControllerDelegate
{
#IBOutlet weak var firstNameTextField: UITextField!
#IBOutlet weak var secondNameTextField: UITextField!
var firstNames = ["Lewis", "Jason","Alex","Mason"]
var secondNames = ["Davies", "Jones","Rees","Kristensen"]
var pickerView = UIPickerView()
override func viewDidLoad()
{
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
firstNameTextField.inputView = pickerView
firstNameTextField.textAlignment = .center
firstNameTextField.placeholder = "Select Name"
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return firstNames.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return firstNames[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
firstNameTextField.text = reports[row]
}
class RefereeViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate, UINavigationControllerDelegate
{
#IBOutlet weak var firstNameTextField: UITextField!
#IBOutlet weak var secondNameTextField: UITextField!
var firstNames = ["Lewis", "Jason","Alex","Mason"]
var secondNames = ["Davies", "Jones","Rees","Kristensen"]
let firstNamePicker = UIPickerView()
let secondNamePicker = UIPickerView()
override func viewDidLoad()
{
super.viewDidLoad()
firstNamePicker.delegate = self
firstNamePicker.dataSource = self
secondNamePicker.delegate = self
secondNamePicker.dataSource = self
firstNameTextField.inputView = firstNamePicker
firstNameTextField.textAlignment = .center
firstNameTextField.placeholder = "Select First Name"
secondNameTextField.inputView = secondNamePicker
secondNameTextField.textAlignment = .center
secondNameTextField.placeholder = "Select Second Name"
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return pickerView == firstNamePicker ? firstNames.count : secondNames.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if pickerView == firstNamePicker {
firstNameTextField.text = firstNames[row]
} else {
secondNameTextField.text = secondNames[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerView == firstNamePicker ? firstNames[row] : secondNames[row]
}
}

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]
}
}

How to use 2 UIPickerViews in the same view controller? [duplicate]

This question already has an answer here:
How to distinguish between multiple uipickerviews on one page
(1 answer)
Closed 5 years ago.
My code below uses a UIPickerView and works perfectly. However I do not know how to repeat this process for 2 different picker views that each contain separate information. l2 and pl2 are the 2nd picker view and label.
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
#IBOutlet var l: UILabel!
#IBOutlet var pl: UIPickerView!
#IBOutlet var l2: UILabel!
#IBOutlet var pl2: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
pl.dataSource = self
pl.delegate = self
pl2.dataSource = self
pl2.delegate = self
}
let choices = ["1","2","3","4","5","6","7","8","9","10","11"]
let choices2 = ["1","judo","3","4","5","6","7","8","9","10","11"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pl {
return choices[row]
} else if pickerView == pl2 {
return choices2[row]
} else {
return nil
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return choices.count
return choices2.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == pl {
l.text = choices[row]
} else if pickerView == pl2 {
l2.text = choices2[row]
}
}
}
You have a reference to your UIPickerView in the delegate methods so you can do code like :
func numberOfComponents(in pickerView: UIPickerView) -> Int {
if(pickerView == pl) {
//pl specific code
}
if(pickerView == pl2) {
//pl2 specific code
}
}
And do that for every method.
You have to set up your ViewController as the DataSource and Delegate of pl2 as well and in the delegate and datasource methods you should filter for the UIPickerView.
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{
#IBOutlet var l: UILabel!
#IBOutlet var pl: UIPickerView!
#IBOutlet var l2: UILabel!
#IBOutlet var pl2: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
pl.dataSource = self
pl.delegate = self
pl2.dataSource = self
pl2.delegate = self
}
let choices = ["1","2","3","4","5","6","7","8","9","10","11"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == pl1 {
return choices[row]
} else if pickerView == pl2 {
return choices2[row] //or whatever you want to use as the dataSource for pl2
} else {
return nil
}
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
//do the same as above
return choices.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
//do the same as above
l.text = choices[row]
}
}

Resources