Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple UIPickerView in the same UIView

I'm a complete beginner on iOS dev and I want to create a little iOS application. On this application, 3 UIPickerViews are supposed to display different data.

My problem is on the display. I'm used to dev on Android or Windows phone and I don't understand how to populate the UIPickerViews with different data.

This is the code I have already written:

//
//  ViewController.swift
//  iphoneVersion
//
//  Created by fselva on 13/05/2015.
//  Copyright (c) 2015 fselva. All rights reserved.
//

import UIKit



class ViewController: UIViewController, UIPickerViewDelegate{



@IBOutlet weak var pickerView1: UIPickerView!
@IBOutlet weak var pickerView2: UIPickerView!
@IBOutlet weak var pickerView3: UIPickerView!

var test = ["Todo","Waiting","Maybe","Inbox","Note"]

var test2 = ["@Office","@Computer","@Home","@Meeting", "@Read", "@Achat", "@Call"]



override func viewDidLoad() {
    super.viewDidLoad()
    pickerView1.tag = 1
    pickerView2.tag = 2
    pickerView3.tag = 3
    // Do any additional setup after loading the view, typically from a nib.
}

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 pickerView2.tag == 2 {
        return test.count
    } else if pickerView3 == 3{
        return test2.count
    }
    return 1
}

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

    if pickerView2 == 2 {
        return test[row]
    } else if pickerView3 == 3{
        return test2[row]
    }
        return ""
}


}

@IBOutlet weak var pickerView1: UIPickerView!has been automatically created by Ctrl+Click from the storyboard to the ViewController.swift.

The first UIPickerView is currently supposed to display nothing. The second one is supposed to display test and the third one displays test2.

After hours of investigation on internet, I have heard about tags to define who must display what, but it doesn't work.

Am I doing something wrong, am I missing something somewhere?

like image 430
fselva Avatar asked May 14 '15 13:05

fselva


3 Answers

Here is Full Source Multiple UIPickerView in the TextField in Swift 3

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var textFiel1: UITextField!

@IBOutlet weak var textFiel_2: UITextField!   


 let piker1 = UIPickerView()
 let piker2 = UIPickerView()

let country = ["Ban","Ind","PK"]
let number = ["1","2","3"]


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    piker1.dataSource = self
    piker1.delegate = self

    piker2.dataSource = self
    piker2.delegate = self


    piker1.tag = 1
    piker2.tag = 2;



     textFiel1.inputView = piker1
      textFiel_2.inputView = piker2


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}



func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {



    if pickerView == piker1 {
        return country.count

    } else if pickerView == piker2{
         return number.count
    }

    return 1
}

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


    if pickerView == piker1 {
        return country[row]

    } else if pickerView == piker2{
         return number[row]
    }
    return ""
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == piker1 {
        textFiel1.text = country[row]
          self.view.endEditing(false)
    } else if pickerView == piker2{
        textFiel_2.text = number[row]
          self.view.endEditing(false)
    }
}

}
like image 34
Enamul Haque Avatar answered Nov 06 '22 01:11

Enamul Haque


You don't need tags, just use:

 func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        if pickerView == pickerView1 {
            //pickerView1
        } else if pickerView == pickerView2{
           //pickerView2
        }

also don't forget to set delegate in IB or in code:

pickerView1.delegate = self
like image 71
ChikabuZ Avatar answered Nov 06 '22 01:11

ChikabuZ


You're comparing instances with tags.

Replace:

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    if pickerView2 == 2 {
        return test[row]
    } else if pickerView3 == 3{
        return test2[row]
    }
        return ""
}

with:

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    if pickerView2.tag == 2 {
        return test[row]
    } else if pickerView3.tag == 3 {
        return test2[row]
    }
        return ""
}

Anyway, you don't need to tag your pickers. The pickers come in the datasource and delegate methods. You can compare instances. Something like this:

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    if pickerView2 == pickerView {
        return test[row]
    }
    if pickerView3 == pickerView {
        return test2[row]
    }
    return ""
}
like image 2
kanobius Avatar answered Nov 06 '22 02:11

kanobius