I've tagged the PickerViews 1 and 2 but I'm getting a "will not be executed" message on the func titleForRow, how do I solve this?
#IBOutlet weak var shoeDetailPic: UIImageView!
#IBOutlet weak var shoeDetailName: UILabel!
#IBOutlet weak var shoeDetailPrice: UILabel!
#IBOutlet weak var shoeColourPickerView: UIPickerView!
#IBOutlet weak var shoeSizePickerView: UIPickerView!
var pickerColour = [""]
var pickerSize = [""]
var getName = String()
var getPrice = String()
var getImage = UIImage()
override func viewDidLoad() {
super.viewDidLoad()
shoeDetailPic.image = getImage
shoeDetailName.text = getName
shoeDetailPrice.text = getPrice
pickerColour = ["Gold", "Black", "Red"]
pickerSize = ["35", "36", "37", "38", "39", "40", "41", "42"]
}
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 {
if (pickerView.tag == 1){
return pickerColour.count
}else{
return pickerSize.count
}
Message here - will not be executed. Is it something to do with the IBOutlet names?
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
if (pickerView.tag == 1){
return "\(pickerColour[row])"
}else{
return "\(pickerSize[row])"
}
}
}
}
Problem is you have put titleForRow delegate method inside the numberOfRowsInComponent, it should be outside of that method as instance method of class.
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if (pickerView == shoeColourPickerView){
return pickerColour.count
}else{
return pickerSize.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView == shoeColourPickerView){
return pickerColour[row]
}else{
return pickerSize[row]
}
}
Note: You have already created outlet for both pickerView then instead of comparing tag in delegate method it is batter if you compare with your outlet.
Related
I have been trying to figure out this problem for hours, now I have only one small problem remaining:
I use 2 UI pickers on one view controller, and when I try to select one (aka I'm clicking into the textfield) all I see is question marks, but if I click on one of them, the text will appear in the textfield. I just don't see what am I choosing in the picker.
I already tried using normal pickerviews, I used tags for each pickers, but nothing seemed to work. I know how it should work, I watched a ton of tutorials, but something is still missing. Can you please help me? Thank you!
Here is the code:
import UIKit
class SelectionViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
#IBOutlet weak var ageTextField: UITextField!
#IBOutlet weak var weightTextField: UITextField!
#IBOutlet weak var genderTextField: UITextField!
#IBOutlet weak var lifeStyleTextField: UITextField!
let picker1 = UIPickerView()
let picker2 = UIPickerView()
var genders = ["Male", "Female"]
var lifeStyle = ["Sitting", "Normal", "Active"]
override func viewDidLoad() {
super.viewDidLoad()
picker1.dataSource = self
picker1.delegate = self
picker2.dataSource = self
picker2.delegate = self
genderTextField.inputView = picker1
lifeStyleTextField.inputView = picker2
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func numberOfComponentsInPickerView(pickerView : UIPickerView!) -> Int{
return 2
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView == picker1 {
return genders.count
} else {
return lifeStyle.count
}
}
func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
if pickerView == picker1 {
return genders[row]
} else if pickerView == picker2{
return lifeStyle[row]
}
else {
print("No pickerview selected.")
}
return ("Pickeview not selected")
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker1 {
genderTextField.text = genders[row]
self.view.endEditing(false)
} else if pickerView == picker2{
lifeStyleTextField.text = lifeStyle[row]
self.view.endEditing(false)
}
else {
print("Love love love I want your love")
}
}
}
And here's the thing I see:
You need this signature ( missed _ )
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
and numberOfComponentsInPickerView should return 1
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()
}
}
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]
}
}
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]
}
}
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])
}