如何在swift中制作2个或更多的选择器视图

时间:2018-04-30 01:45:04

标签: swift uipickerview

所以我想制作2个选择器视图,但我的问题是每次我调用文本字段时他们只调用一个pickerview我已经尝试替换并使2个函数具有不同的值但是当我运行它时函数不起作用 这里是我的代码我认为问题是因为pickerview函数,但我不知道如何替换代码添加条件如果用户单击jenisIDTextField pickerview将显示jenisID数组,如果用户选择provinisTextField,则pickerview将显示provinsi数组

我感谢所有答案 谢谢你的帮助!

@IBOutlet weak var provinsiIdentitasTextField:
@IBOutlet weak var jenisIDTextField:

func choosePicker(){

    let pickerView = UIPickerView()

    pickerView.delegate = self


jenisIDTextField.inputView=pickerView
provinsiIdentitasTextField.inputView = pickerView

}

 let provinsi=["DKI Jakarta","Jawa Barat","Sumatra Utara"]
    let JenisID=["KTP","SIM","Passport"]
var asd = 0

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

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



        return provinsi[row]

}


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

}

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


        return provinsiIdentitasTextField.text=provinsi[row]


}

4 个答案:

答案 0 :(得分:1)

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

    if pickerView == pickerView1 {
        //pickerViewdetails
    } else if pickerView == pickerView2{
       //pickerView2details
    }}

您可以公开声明pickerview,并使用上述代码添加多个pickerview。

答案 1 :(得分:0)

试试吧,希望它能为你效劳。

    //declare variable
        let yourArray : [String] = []

    //implement textfiled delegate
            func textFieldDidBeginEditing(_ textField: UITextField) {
                if textField == provinisTextField{
                    yourArray = ["DKI Jakarta","Jawa Barat","Sumatra Utara"]
                }else if textField == jenisIDTextField{
                    yourArray = ["KTP","SIM","Passport"]
                }
            }


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

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

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

答案 2 :(得分:0)

这是完整的实施准则:

class ViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate {

@IBOutlet weak var textFieldOne: UITextField!
@IBOutlet weak var textFieldTwo: UITextField!

@IBOutlet weak var pickerView: UIPickerView!

var isForWhichTextField:Int = 0

var arrForDisplay:[String] = [String]()

let provinsi = ["DKI Jakarta","Jawa Barat","Sumatra Utara"]
let JenisID = ["KTP","SIM","Passport"]


override func viewDidLoad() {
    super.viewDidLoad()
    textFieldOne.inputView = pickerView
    textFieldTwo.inputView = pickerView
    arrForDisplay = provinsi
    // 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 pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
   return arrForDisplay.count
}

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

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

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

//MARK:- UITextfields Delegates

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
    if textField == textFieldOne{
        arrForDisplay = provinsi
        pickerView.reloadAllComponents()
    }else{
        arrForDisplay = JenisID
        pickerView.reloadAllComponents()
    }
    return true
}}

希望这有帮助。

答案 3 :(得分:0)

@ Jaydip答案的一个小修正。

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

这总是只为您选择的文本字段赋予provinsiIdentitasTextField文本字段的值。所以创建一个新的文本字段并将选定的文本字段分配给

      var selectedField: UItextField?

     //implement textfiled delegate
                func textFieldDidBeginEditing(_ textField: UITextField) {

                    if textField == provinisTextField{
                        yourArray = ["DKI Jakarta","Jawa Barat","Sumatra Utara"]
                    }else if textField == jenisIDTextField{
                        yourArray = ["KTP","SIM","Passport"]
                    }
              selectedField = textField

            pickerView.reloadAllComponents()
                }

///分配选定的文本字段

 func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
                return selectedField!.text= yourArray[row]
        }