UIViewController with multiple UIPickerViews and arrays - ios

I am trying to get two UITextFields to show a UIPickerView with different arrays when pressed.
Currently, I have the following UIPickerView show the array pickOption when pickerTextField is pressed.
How can I have my TextField pickerTextField2 show the array pickOptions2 when pressed, all on the same UIViewController.
I understand I have to
Code So far:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
#IBOutlet weak var pickerTextField: UITextField!
#IBOutlet weak var pickerTextField2: UITextField!
var pickOption = ["one", "two", "three", "four", "five"]
var pickOption2 = ["two", "four", "six", "eight", "ten"]
override func viewDidLoad() {
super.viewDidLoad()
let pickerView = UIPickerView()
pickerView.delegate = self
pickerTextField.inputView = pickerView
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickOption.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickOption[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pickerTextField.text = pickOption[row]
}
}

Assign tag to your IBOutlet TextField and also take one more textfield variable for assigning current selected textfield.
I Write example here based on your code.Using this logic you can implement picker view functionality with many more textfield.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
#IBOutlet weak var pickerTextField: UITextField!
#IBOutlet weak var pickerTextField2: UITextField!
var textField1: UITextField?
var pickOption = ["one", "two", "three", "four", "five"]
var pickOption2 = ["two", "four", "six", "eight", "ten"]
let pickerView = UIPickerView()
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
textField1 = textField
pickerView.reloadAllComponents()
return true
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerTextField.delegate = self
pickerTextField2.delegate = self
pickerTextField.inputView = pickerView
pickerTextField2.inputView = pickerView
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if textField1?.tag == 10 {
return pickOption.count
}
return pickOption2.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if textField1?.tag == 10 {
return pickOption[row]
}
return pickOption2[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if textField1?.tag == 10 {
textField1?.text = pickOption[row]
return
}
textField1?.text = pickOption2[row]
}
}

Related

How to remove and add items in uipickerview in swift 4.?

I have three text fields for which I am showing the same pickerview as drop down for choosing the values. So the condition is that I have 5 values in an array with values red, blue, green, yellow, black.
So the condition is
These three text fields cannot have same value. That is if for first textfield 1 if I choose red as value from picker the value " red " should be removed or disabled from picker view when I select textfield 2 or text field 3 .
And if I change the value of textfield 1 from red to black from picker view the value red which is disabled or removed should get added back to the pickerview when I click on textfield 2 or textfield 3.
The code which I am trying is:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
#IBOutlet weak var textFiled1: UITextField!
#IBOutlet weak var textFiled2: UITextField!
#IBOutlet weak var textFiled3: UITextField!
#IBOutlet weak var pickerView: UIPickerView!
var Array = ["Blue", "Green", "Red", "White", "Grey"]
var indexOfPicker = Int()
override func viewDidLoad() {
super.viewDidLoad()
pickerView.dataSource = self
pickerView.delegate = self
}
#IBAction func minusButton(_ sender: UIButton) {
if Array.count != 0 {
Array.remove(at: indexOfPicker)
pickerView.reloadAllComponents()
}
}
#IBAction func plusButton(_ sender: UIButton) {
if textFiled.text != "" {
Array.append(textFiled.text!)
pickerView.reloadAllComponents()
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return Array.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return Array[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
indexOfPicker = row
}
}
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate, UITextFieldDelegate {
#IBOutlet weak var textFiled1: UITextField!
#IBOutlet weak var textFiled2: UITextField!
#IBOutlet weak var textFiled3: UITextField!
#IBOutlet weak var pickerView: UIPickerView!
var selectedTextField:UITextField?
var colorsArray = ["Blue", "Green", "Red", "White", "Grey"]
override func viewDidLoad() {
super.viewDidLoad()
textFiled1.delegate = self
textFiled2.delegate = self
textFiled3.delegate = self
pickerView.dataSource = self
pickerView.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
let tempArr = colorsArray.filter { ![textFiled1.text!,textFiled2.text!,textFiled3.text!].contains($0) }
return tempArr.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
let tempArr = colorsArray.filter { ![textFiled1.text!,textFiled2.text!,textFiled3.text!].contains($0) }
return tempArr[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let tempArr = colorsArray.filter { ![textFiled1.text!,textFiled2.text!,textFiled3.text!].contains($0) }
self.selectedTextField?.text = tempArr[row]
pickerView.reloadAllComponents()
}
func textFieldDidBeginEditing(_ textField: UITextField) {
self.selectedTextField = textField
pickerView.reloadAllComponents()
}
}

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

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

IOS swift UIPickerview issue

How can I add other picker view in my view controller
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var UnitTypeLabel: UILabel!
#IBOutlet weak var UnitTypeSelectBTN: UIButton!
#IBOutlet weak var UnitTypePicker: UIPickerView!
let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]
let UnitforLengthData = ["m","km","cm","mm","yd","ft","in"]
let UnitforWeightData = ["g","kg","m.t","l.t","sh.t","lb"]
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return UnitDataArray.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return UnitDataArray[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
return UnitTypeLabel.text = UnitDataArray[row]
}
override func viewDidLoad() {
super.viewDidLoad()
//UnitTypePicker.hidden = true
UnitTypePicker.delegate = self
UnitTypePicker.dataSource = self
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Here is how it should be, you have to implement pickerView viewForRow method to achieve this:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
#IBOutlet weak var pickerView: UIPickerView!
let UnitDataArray = ["Unit for Weight","Unit for Length","Unit for Area","Unit for Volume","Unit for Quantity"]
override func viewDidLoad() {
super.viewDidLoad()
// pickerView delegate n dataSource
pickerView.dataSource = self
pickerView.delegate = self
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return UnitDataArray.count
}
//MARK: Delegates
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return UnitDataArray[row]
}
func pickerView(pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusingView view: UIView!) -> UIView
{
var pickerLabel = UILabel()
pickerLabel.textColor = UIColor.blackColor()
pickerLabel.text = UnitDataArray[row]
if let pickerViewFont = UIFont(name: "HelveticaNeue-Thin", size: 30) {
pickerLabel.font = pickerViewFont
}
pickerLabel.textAlignment = NSTextAlignment.Center
return pickerLabel
}
func pickerView(pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
return 40.0
}
}
If you want more number of pickerView then you will have to set those number of components in it like this:
func numberOfComponentsInPickerView(pickerView: UIPickerView!) -> Int {
return 2 //number of pickerViews
}
By changing the number, you can set those number of pickerView in same viewController.
But if you want to change only dataSet for only one pickerView after any action then you can see this How do I reload/refresh the UIPickerView (with new data array) based on button press?

UIPickerView showing empty grey box Swift

I haven't a whole lot of code, so I might as well copy it here.
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource{
var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]
#IBOutlet weak var buildText: UITextField!
var buildPickers:UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.hidden = true;
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
println("Count: \(buildings.count)")
return buildings.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
println("creating title: \(buildings[row])")
return buildings[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
println("Selected: \(buildings[row])")
buildText.text = buildings[row]
buildPickers.hidden = true;
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
buildPickers.hidden = false
return false
}
}
The print statements are correct. For numberOfRows... and titleForRow it is printing the correct Strings.
But there is no prints for didSelectRow because, well, I can't select a row.
This is what I get:
You can ignore the Google Map in the background, that shouldn't interfere with the Picker View and is just set up in the StoryBoard.
The Grey window appears when I click on the textField but never shows any content. But the print statements say otherwise.
Does anyone know why this is the case?
Just add this line in your viewDidLoad method:
buildPickers.dataSource = self
And your code will be:
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.dataSource = self
buildPickers.hidden = true;
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
And it will show your data.
UPDATE:
It is not showing because you set it hidden in your viewDidLoad.
Just remove this line from your code:
buildPickers.hidden = true
Here is your working code:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate{
var buildings = ["BankBuilding", "Cinema" , "CornerShop", "Greg's House"]
#IBOutlet weak var buildText: UITextField!
var buildPickers:UIPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
buildPickers = UIPickerView()
buildPickers.delegate = self
buildPickers.hidden = true
buildText.delegate = self //set delegate for textField
buildText.inputView = buildPickers
buildText.text = buildings[0]
}
func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
println("Count: \(buildings.count)")
return buildings.count
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
println("creating title: \(buildings[row])")
return buildings[row]
}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
println("Selected: \(buildings[row])")
buildText.text = buildings[row]
buildPickers.hidden = true;
}
//this method will call when you click on textField
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
buildPickers.hidden = false
return true
}
}
Is the delegate set on the text field? Are you getting the callback which you use to un-hide the picker?

Resources