0

是否有可能pickerView在 swift 中根据按下的按钮更改其值。我想不出一种方法来通过pickerView单个ViewController. 像 a 这样的替代解决方案dropdown menu也很好。如果提供示例/功能代码会很棒。这是一个需要多个参数的测验应用程序。

在此处输入图像描述

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


}

}
4

6 回答 6

0

每当您必须更改日期选择器的数据源时,请确保在此之后您正在调用方法

picker.reloadAllComponents()

取标志并使用适当的数据源。

于 2018-04-19T11:43:24.277 回答
0

假设您在视图中添加了 2 PickerView - 将第一个选择器的标签设置为 1 和 2 用于属性检查器下的第二个选择器 - CTRL+ 从每个选择器拖动到顶部的黄色视图控制器图标并选择 dataSource。重复相同的选择委托 - 对其他选择器也重复上述操作 - 将 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])"
        }
    }
    

希望这会有所帮助。

于 2018-04-19T11:38:24.747 回答
0

是,拥有超过 1 个数据源

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()
于 2018-04-19T11:40:02.310 回答
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


}
于 2018-04-19T11:40:57.797 回答
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]
        }
    }
于 2018-04-19T11:40:58.083 回答
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]
}
于 2018-04-19T11:52:31.510 回答