Example of Picker View in Swift

Asked

Viewed 832 times

0

Dear friends, how to popular an Uipickerview object with the simple array?

example:

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

how to set this array to a Uipickerview?

  • The procedure is the same for a tableView. What you tried?

2 answers

1

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

Declare the protocols:

`class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate`

Have a global variable containing the securities that will be used in Picker:

var titulosArray:NSArray = []

override func viewDidLoad()
{
    super.viewDidLoad()

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

And then implement the protocols to fill 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 take it here: http://github.com/xdoug/SwiftPickerView

0

It will be necessary to implement UIPickerViewDelegate and UIPickerViewDataSource.

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

I will illustrate.

Protocol of the two necessary 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
    }
}

Implementation of the Protocol

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

Browser other questions tagged

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