How to distinguish between multiple uipickerviews on one page

孤街浪徒 提交于 2019-12-29 09:57:08

问题


  • I have 2 uipickerviews in one uiviewcontroller:

dropdown_1.frame=CGRectMake(...); dropdown_2.frame=CGRectMake(...);

  • I have 2 different array of values for these 2 dropdowns
> arraySizes5 = [sverysmall, ssmall, snormal, slarge, sverylarge];

> arraySizes3 = [ ssmall, snormal, slarge];
  • I have function like :

.

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

func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return arraySizes5.count;  <<----- How to differentiate ??
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
    return arraySizes5[row]
}

How can I add the corresponding values to the 2 dropdowns in the function pickerView ? Or how can I set the component ID if that helps ?


回答1:


Here is the way you can do it:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var dropdown_1: UIPickerView!
    @IBOutlet weak var dropdown_2: UIPickerView!

    var arraySizes5 = ["sverysmall", "ssmall", "snormal", "slarge", "sverylarge"]
    var arraySizes3 = ["ssmall", "snormal", "slarge"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // 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 pickerView == dropdown_1 {
            return arraySizes5.count
        } else if pickerView == dropdown_2 {
            return arraySizes3.count
        }
        return 0
    }

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

        if pickerView == dropdown_1 {
            return arraySizes5[row]
        } else if pickerView == dropdown_2 {
            return arraySizes3[row]
        }
        return ""
    }
}


来源:https://stackoverflow.com/questions/31200987/how-to-distinguish-between-multiple-uipickerviews-on-one-page

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