Swift Picker View Example

0

Dear friends, how would you make a UIPickerView object with the simple array popular?

example:

var titulos = ["texto1", "texto2", "texto3", "texto4"]

How to set this array in a UIPickerView?

    
asked by anonymous 10.09.2014 / 00:17

2 answers

1

You just need to set the delegate and datasource of UIPickerView in your viewcontroller (this can be done by storyboard )

Declare the protocols:

'class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate'

Have a global variable that contains the titles that will be used in the picker:

var titulosArray:NSArray = []

override func viewDidLoad()
{
    super.viewDidLoad()

    self.titulosArray = ["texto1", "texto2", "texto3", "texto4"]
}

And then implement the protocols to fill the picker:

// MARK: - UIPickerViewDelegate

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
    // Número total de componetes na picker view
    return 1
}

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
    // Número total de itens na picker view
    return self.titulosArray.count
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
{
    // Título com a posição do array correspondente com a linha do picker
    return self.titulosArray[row] as NSString
}

And that's it. If you want to take a look at this working example, just get it here: link

    
10.09.2014 / 04:39
0

You will need to implement UIPickerViewDelegate and UIPickerViewDataSource .

With Swift I try to use extension because it makes the code more explicit so as to know the methods inherited from each class. I also like to create a protocol to group the two classes together.

I'll illustrate.

Protocol of the two required classes

protocol PickerProtocol: UIPickerViewDelegate, UIPickerViewDataSource
{
    // Retorna o número de colunas
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int

    // Retorna os # elementos em cada componente
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int

    // Retorna o texto a mostrar em cada componente
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
}

View Controller

class ExemploViewController: UIViewController
{
    var elementos: Array<String> = [] //Array nativo de Swift

    override func viewDidLoad()
    {
        super.viewDidLoad()

        elementos.extend(["Texto1", "Texto2", "Texto3"])

        // Criar picker view
        let pickerView = UIPickerView()
        self.view.addSubview(pickerView)

        pickerView.delegate = self
        pickerView.dataSource = self
    }
}

Protocol Implementation

// MARK: - PickerProtocol

extension ExemploViewController: PickerProtocol
{
    // Retorna o número de colunas
    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
    {
        return 1;
    }

    // Retorna os # elementos em cada componente
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
    {
        return elementos.count;
    }

    // Retorna o texto a mostrar em cada componente
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
    {
        return elementos[row]
    }

    // Ao seleccionar um elemento
    func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
    {
        NSLog("%@", elementos[row])
    }
}
    
10.09.2014 / 11:07