How to use UIPickerView with TextField more than once? - ios

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

Related

Swift 4.2 - PickerView not showing Content

this code makes me really mad. I already read thousands of tutorials and stack overflow comments....
Please help me. The Picker just contains a blank grey screen.
Code here:
GenderPickerModel.swift
import UIKit
class GenderPickerModel: NSObject, UIPickerViewDataSource, UIPickerViewDelegate {
let genderPickerData: [String] = ["male", "female"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1;
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return genderPickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return genderPickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
}
and used in a Controller:
#IBOutlet weak var gender: UITextField!
private var genderPickerView: UIPickerView?
override func viewDidLoad() {
super.viewDidLoad()
loadProfile()
prepareView()
}
func prepareView() {
genderPickerView = UIPickerView.init()
let gpModel = GenderPickerModel()
genderPickerView?.delegate = gpModel
genderPickerView?.dataSource = gpModel
gender.inputView = genderPickerView;
genderPickerView?.selectRow(0, inComponent: 0, animated: true)
genderPickerView?.reloadAllComponents()
}
First, you do not need to do the separate array of gender in pickerview class and you don't need to create the instance of pickerview.
From this, you can see your datasource in your custompickerview
/**
Controller code where you can create instantiate picker assign data
*/
class ViewController: UIViewController {
#IBOutlet weak var textField: UITextField!
let genderPickerData : [String] = ["male", "female"]
override func viewDidLoad() {
super.viewDidLoad()
prepareView()
}
func prepareView() {
genderPickerView = UIPickerView()
let gpModel = GenderPickerModel()
gpModel.data = genderPickerData
gpModel.dataSource = gpModel
gpModel.delegate = gpModel
textField.inputView = gpModel
}
}
/**
Custom pickerview class where you can handle data
*/
import UIKit
class GenderPickerModel: UIPickerView, UIPickerViewDataSource, UIPickerViewDelegate {
var data = [String]()
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return data.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return data[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
}
}

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

My selector lines are only displaying one line in my Pickers. I'd like them to be like how the date picker is

For some reason I can't seem to get both selector lines in my pickers to show. They only seem to show one but the date picker is fine.
class HomeViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var linePicker: UIPickerView!
#IBOutlet weak var skuPicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
linePicker.delegate = self
linePicker.dataSource = self
skuPicker.delegate = self
skuPicker.dataSource = self
//passed over from previous view
var plantInfo = plantInformation
let totalNumberOfLines = plantInfo!["lines"].count
//populated the data
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView.isEqual(linePicker)){
return lineInfo.count
} else {
return skusForPickerLine.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.isEqual(linePicker) {
return lineInfo[row].lineNumber
} else {
return skusForPickerLine[row].sku_value
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedValue = linePicker.selectedRow(inComponent: 0)
skusForPickerLine = lineInfo[selectedValue].skuInfo
skuPicker.reloadComponent(0)
}
func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
// height of picker is 50
return CGFloat(50)
}
}

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

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