How to use 2 UIPickerViews in the same view controller? [duplicate]

本秂侑毒 提交于 2019-12-12 04:15:23

问题


My code below uses a UIPickerView and works perfectly. However I do not know how to repeat this process for 2 different picker views that each contain separate information. l2 and pl2 are the 2nd picker view and label.

    import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

@IBOutlet var l: UILabel!
@IBOutlet var pl: UIPickerView!
    @IBOutlet var l2: UILabel!
    @IBOutlet var pl2: UIPickerView!
override func viewDidLoad() {
    super.viewDidLoad()
    pl.dataSource = self
    pl.delegate = self
    pl2.dataSource = self
    pl2.delegate = self

}

    let choices = ["1","2","3","4","5","6","7","8","9","10","11"]
    let choices2 = ["1","judo","3","4","5","6","7","8","9","10","11"]

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

}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == pl {
        return choices[row]
    } else if pickerView == pl2 {
        return choices2[row]
    } else {
        return nil
    }
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {


return choices.count
    return choices2.count

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


    if pickerView == pl {
         l.text = choices[row]
    } else if pickerView == pl2 {
            l2.text = choices2[row]
    }
}

}

回答1:


You have a reference to your UIPickerView in the delegate methods so you can do code like :

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    if(pickerView == pl) {
      //pl specific code
    }
    if(pickerView == pl2) {
       //pl2 specific code
    }
}

And do that for every method.




回答2:


You have to set up your ViewController as the DataSource and Delegate of pl2 as well and in the delegate and datasource methods you should filter for the UIPickerView.

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate{

@IBOutlet var l: UILabel!
@IBOutlet var pl: UIPickerView!
@IBOutlet var l2: UILabel!
@IBOutlet var pl2: UIPickerView!

override func viewDidLoad() {
    super.viewDidLoad()
    pl.dataSource = self
    pl.delegate = self
    pl2.dataSource = self
    pl2.delegate = self
}

let choices = ["1","2","3","4","5","6","7","8","9","10","11"]

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

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == pl1 {
        return choices[row]
    } else if pickerView == pl2 {
        return choices2[row] //or whatever you want to use as the dataSource for pl2
    } else {
        return nil
    }
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    //do the same as above
    return choices.count
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    //do the same as above
    l.text = choices[row]
}

}


来源:https://stackoverflow.com/questions/45194543/how-to-use-2-uipickerviews-in-the-same-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!