拥有多个Picker视图

时间:2018-02-19 04:11:18

标签: ios swift user-interface uipickerview

我试图在swift中的一个视图中有多个选择器视图。 到目前为止,我已经创建了2个不同的文本字段,如果我点击它们,我希望它们都有不同的选择器视图。所以,让我们说如果单击文本字段#1,则打开数组#1,将文本字段#2打开。

我已经查过这些问题,但他们并没有真正回答我的问题或解决我的问题:

Multiple Picker Views on a single view

Creating Multiple Dynamic Picker Views

所以我的View Controller看起来像这样:

how my viewController should look

这是我的代码:

let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()

//picker View functions
func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return 1
}

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

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    weightTextField.text = pickOption[row]
}

override func viewDidLoad() {
    super.viewDidLoad()
    pickerView.delegate = self
    pickerView.dataSource = self
    weightTextField.inputView = pickerView
    weightTextField.text = pickOption[0]
} 

如前所述,第一个文本字段打开数组'pickOption',第二个数组打开'ageOption'

编辑:'weightTextField'是打开第一个数组的第一个文本字段(pickOption)

3 个答案:

答案 0 :(得分:2)

我们可以在UITextField

的帮助下,从单PickerView多个UITextFieldDelegate进行此操作
let pickOption = ["kg","lb"] //first array for first textfield
let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
let pickerView = UIPickerView()

var currentTxtFldTag : Int = 10

override func viewDidLoad() {
    super.viewDidLoad()

    pickerView.delegate = self
    pickerView.dataSource = self

    weightTextField.tag = 10
    ageTextField.tag = 20

    weightTextField.delegate = self
    ageTextField.delegate = self

    weightTextField.inputView = pickerView
    weightTextField.text = pickOption[0]
    ageTextField.inputView = pickerView
    ageTextField.text = ageOption[0]

    // Do any additional setup after loading the view.
}

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

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

    if currentTxtFldTag == 10
    {
        return pickOption.count
    }
    else
    {
        return ageOption.count
    }

}

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

    if currentTxtFldTag == 10
    {
        return pickOption[row]
    }
    else
    {
        return ageOption[row]
    }

}

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

    if currentTxtFldTag == 10
    {
        weightTextField.text = pickOption[row]
    }
    else
    {
        ageTextField.text = ageOption[row]
    }


}

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {

    if textField.tag == 10  // WEIGHT OPTION
    {
        currentTxtFldTag = 10
    }
    else  // AGE OPTION
    {
        currentTxtFldTag = 20
    }

    pickerView.reloadAllComponents()
    return true
}

<强>输出

enter image description here

答案 1 :(得分:1)

假设您有两个pickerView控件:picker0和picker1。

class HomeViewController: BasicViewController, UITextFieldDelegate, UITextViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource {
    let numberArray = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180]
    var areaArray = [String]()

    @IBOutlet weak var picker0: UIPickerView!
    @IBOutlet weak var picker1: UIPickerView!

    override func viewDidLoad() {
        super.viewDidLoad()    
        // area //
        for i in 0..<11 {
            let str = "Area "
            let localStr = NSLocalizedString(str + String(i), comment: "")
            areaArray.append(localStr)
        }
    }

    // MARK: - Campaign duration & area
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView == picker0 {
            return numberArray.count
        } else {
            return areaArray.count
        }
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView == picker0 {
            return String(numberArray[row]) + " pieces"
        } else {
            return areaArray[row]
        }
    }

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

        } else {

        }
    }
}

答案 2 :(得分:0)

override func viewDidLoad() {
    super.viewDidLoad()
    pickerView.delegate = self
    pickerView.dataSource = self
    weightTextField.inputView = pickerView

} 

    let pickOption = ["kg","lb"] //first array for first textfield
    let ageOption = ["0-5", "6-10", "11-15"] //array for 2nd textfield
    let pickerView = UIPickerView()

    //picker View functions
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

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

        if weightTextField1.tag == 1 {
            return pickOption.count
        } else if weightTextField2.tag == itemPicker {
            return ageOption.count
        }
        return 0
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
          if weightTextField1.tag == 1 {
            return pickOption[row]
        } else if weightTextField2.tag == 1 {
            return ageOption[row]
        }
        return 0
    }

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

          if weightTextField1.tag == 1 {
                weightTextField1.text = pickOption[row]
        } else if weightTextField2.tag == 1 {
        weightTextField2.text = ageOption[row]
        }

    }


    ////set delegate 

    func textFieldDidBeginEditing(textField: UITextField!) {    //delegate method

      if textField == weightTextField1
    {
            weightTextField1.atg = 1;
            weightTextField2.atg = 0;
        }
    else  if textField == weightTextField2
    {
       weightTextField1.atg = 0;
      weightTextField2.atg = 1;
    }
        picker.reloadAllComponents()
       }
    }