单个viewController中的多个pickerviews

时间:2018-04-19 11:33:39

标签: ios swift pickerview

是否可以在swift中使用单个pickerView,根据按下的按钮更改其值。我无法找到在pickerView中通过ViewController实现多个参数的方法。像dropdown menu这样的替代解决方案也很好。如果提供的话,示例/功能代码会很棒。这是一个测验应用程序,它将采用多个参数。

enter image description here

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var pickerView: UIPickerView!
@IBOutlet weak var selectionBtn: UIButton!

let animals = ["Lion", "Dog", "Monkey", "Cat", "Bat"]

override func viewDidLoad() {

    pickerView.isHidden = true

    pickerView.delegate = self
    pickerView.dataSource = self

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

@IBAction func selectPressed(_ sender: UIButton) {

    if pickerView.isHidden {
        pickerView.isHidden = false
    }
}

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

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return animals.count
}

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

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

    selectionBtn.setTitle(animals[row], for: .normal)
    pickerView.isHidden = true


}

}

6 个答案:

答案 0 :(得分:0)

假设您在视图中添加了2个PickerView   - 将第一个选择器的标签设置为1& 2为属性检查器下的第二个选择器   - CTRL +从每个选择器拖动到顶部黄色View Controller图标并选择dataSource.Repeate选择相同的委托   - 也为其他选择器重复上述内容   - 添加pickerview& pickerviewdelegation到您的ViewController类:

class ViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate {
    在ViewController类中,
  • 为拾取器创建空数组:

    var picker1Options = []
    var picker2Options = []
    
  • 在viewDidLoad()方法中,使用您的内容填充数组:

    picker1Options = ["p1 1","p1 2","p1 3","p1 4","p1 5"]
    picker2Options = ["p2 1","p2 2","p2 3","p2 4","p2 5"]
    
  • 实施代表& pickerview方法:

    func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if (pickerView.tag == 1){
            return picker1Options.count
        }else{
            return picker2Options.count
        }
    }
    
    func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
        if (pickerView.tag == 1){
            return "\(picker1Options[row])"
        }else{
            return "\(picker2Options[row])"
        }
    }
    

希望这会有所帮助。

答案 1 :(得分:0)

是的,拥有多个数据源

let animals1 = ["Lion", "Dog", "Monkey", "Cat", "Bat"]

let animals2 = ["Lion", "Dog", "Monkey", "Cat", "Bat"]

var mainData = [String]()

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

 public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
   return mainData.count
 }

 func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, fo rComponent component: Int) -> String? {
  return mainData[row]
 }

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

   selectionBtn.setTitle(mainData[row], for: .normal)
   pickerView.isHidden = true

 }

指定mainDataanimal1

并重新加载

pickerView.reloadAllComponents()

答案 2 :(得分:0)

保留一个单独的数组,并根据您的逻辑将数组放入其中。在pickerview中使用该数组。试试这个:

let animals = ["Lion", "Dog", "Monkey", "Cat", "Bat"]
let secondAnimals = ["Lion1", "Dog1", "Monkey1", "Cat1", "Bat1"]
let pickerArray = [String]()

@IBAction func selectPressed(_ sender: UIButton) {
    pickerArray = animals  //change it according to your logic
    pickerView.reloadAllComponents()
    pickerView.isHidden = !pickerView.isHidden
}

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerArray.count
}

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

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

    selectionBtn.setTitle(pickerArray[row], for: .normal)
    pickerView.isHidden = true


}

答案 3 :(得分:0)

您可以使用两个数组来填充选择器,如下所示

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

        if numb == 1
        {
          return firstArray.count
        }
        else

           return secondArray.count
        }
    }

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


        if numb == 1
        {
           return firstArr[row]
        }
        else
        {
           return secondArr[row]
        }
    }

答案 4 :(得分:0)

每当您必须更改datepicker的数据源时,请确保在调用方法

之后
picker.reloadAllComponents()

取旗并使用适当的数据源。

答案 5 :(得分:0)

let arrAny : [String] = [String]()

button行动:

@IBAction func btn2Pressed(_ sender: UIButton) {
    arrAny = ["Lion1", "Dog1", "Monkey1", "Cat1", "Bat1"]
    if pickerView.isHidden {
         pickerView.isHidden = false
    }
}

@IBAction func btn1Pressed(_ sender: UIButton) {
     arrAny = ["Lion", "Dog", "Monkey", "Cat", "Bat"]
     if pickerView.isHidden {
          pickerView.isHidden = false
     }
}

pickerView代表:

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

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return arrAny.count
}

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