How to put more than one pickerView in just one Viewcontroller

Asked

Viewed 160 times

1

I’m trying to build a View that takes user data such as height, weight and age using Pickerviews.

It is not returning any errors, but when I run, the Windows pickers only have a question mark, according to the photo. In the print, you are returning the correct number of [Row], but not on the screen. Below 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? From now on thank you! inserir a descrição da imagem aqui

3 answers

0

Thanks a lot, Ricardo! Really that way it worked too! But I got another way rs

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

Thank you, unfortunately I am new to Stackoverflow and I cannot count my vote in your reply :/

//Function that assigns titles to each pickerView Row 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")

                    }
                }

0

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

PickerView.tag = 1

And there’s no need for you to use String to convert.

0


I’m new to this Swift business... and I’m also picking it up for set up a pickerview. I took your code and... What you demonstrated really happened. Then 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 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 out perfectly.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.