How to put more than one pickerView in just one ViewController

1

I'm trying to mount a View that takes user data, such as height, weight, and age, using PickerViews.

There is no error returning, but when I execute, the pickerViews will only have one question, depending on the photo. In print, I am returning the correct number of [row], but not on the screen. Here is part of the code:

class IMCViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource
{

    let arrayAnos : [Int] = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
    let arrayAltura: [Int] = [50, 51,52,53,54,55,56,57,58,59,60]
    let arrayPeso: [Int] = [30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52]


override func viewDidLoad() {
        super.viewDidLoad()

        self.pickerViewPeso.delegate = self
        self.pickerViewPeso.dataSource = self

        self.pickerViewAltura.delegate = self
        self.pickerViewAltura.dataSource = self

        self.pickerViewIdade.delegate = self
        self.pickerViewIdade.dataSource = self




  // MARK: - Métodos de UIPickerViewDataSource

    // Método que define a quantidade de components (colunas) do pickerView
    func numberOfComponents(in pickerView: UIPickerView) -> Int {

        if pickerView == pickerViewAltura {
      //      return arrayAltura.count
        return 1
        }
        else if pickerView == pickerViewPeso {
        return 1

        } else {
        return 1
        }
    }

    // Método que define a quantidade de linhas para cada component
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        if pickerView == pickerViewAltura
        {
            return self.arrayAltura.count
        }
        else if pickerView == pickerViewPeso
        {
            return self.arrayPeso.count
        }
        else
        {
            return self.arrayAnos.count
        }
    }


    // MARK: - Métodos de UIPickerViewDelegate

   func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int! {
                if pickerView == pickerViewPeso {
                return self.arrayPeso[row]
            }
                if pickerView == pickerViewAltura {
                return self.arrayAltura[row]
            }
                else{

                return self.arrayAnos[row]
            }
            }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        if pickerView == pickerViewPeso {
        print("Peso Selecionada: \(row)")
           pesoUsuario = arrayPeso[row]
           print ("O peso do usuário é: \(pesoUsuario)")
        }
        else if pickerView == pickerViewAltura {
        print("Altura Selecionada: \(row)")
        alturaUsuario = arrayAltura[row]
            print ("Altura do usuário: \(alturaUsuario)")
        }
        else{
        print("Idade Selecionada: \(row.description)")
            idadeUsuario = arrayAnos[row]
            print ("Idade do usuário:\(idadeUsuario)")
        }
    }
    }

Can you help me? Thank you!

    
asked by anonymous 05.05.2017 / 20:14

3 answers

0

I'm new to this swift deal ... And I'm also picking up mount a pickerview. I got your code and rode ... Really happened what you demonstrated. So I changed the arrays to String

let arrayAnos : [String] = ["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18"]
let arrayAltura: [String] = ["50", "51","52","53","54","55","56","57","58","59","60"]
let arrayPeso: [String] = ["30","31","32","33","34","35","36","37","38","39","40","41","42","43","44","45","46","47","48","49","50","51","52"]

and I changed the function return

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> Int!

for

 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?

Then everything worked perfectly.

    
05.05.2017 / 22:42
0

Thanks Ricardo! It really worked that way, too! But I did it in another way rs

I just converted the result, because it was returning an Integer in the method, but the titleForRow function returns only Strings and I kept my array with integers, because it would make some calculations that I need to do with the data, so I just converted the result to String as below:

Thank you very much, unfortunately I am new to StackOverflow and I can not count my vote in your answer: /

// Function that assigns titles to each row of the pickerView func pickerView (_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) - > String? {

            if pickerView == pickerViewPeso {

                return String("\(arrayPeso[row]) Kg")
        }
                if pickerView == pickerViewAltura {
                return String("\(arrayAltura[row]) Cm")

                }else{
                if pickerView == pickerViewIdade && arrayAnos[row] == 1 {
                    return String ("\(arrayAnos[row]) ano")
                }else{
                    return String("\(arrayAnos[row]) anos")

                    }
                }
    
08.05.2017 / 18:19
0

You can use the tag property and use switch or if and else to distinguish them.

PickerView.tag = 1

And you do not need to use String to convert.

    
14.10.2017 / 23:00