我试图在同一个viewController上有两个不同的pickerViews,并让它们代表不同的数据。我尝试了不同的方法来执行此操作,但是两个选择器视图都显示相同的数据,但无法将所有内容放在一起。任何帮助都是极好的!

这是我的代码:

 @IBOutlet weak var Eventtype: UITextField!


    let pickerView = UIPickerView()

    var Eventstype = ["Birthday" , "Marriege" , "Get together", "Conference"]

    @IBOutlet weak var datepickertxt: UITextField!

    var datepicker = UIDatePicker()

    @IBOutlet weak var Duration: UITextField!
     let duration = UIPickerView()

    var Durations = ["6-8","1-5","7-9","10-12"]


    override func viewDidLoad() {
        super.viewDidLoad()

        createDatePicker()
        pickerView.delegate = self
        pickerView.dataSource = self
        duration.delegate = self
        duration.dataSource = self
        Eventtype.inputView = pickerView
        Duration.inputView = duration
        // Do any additional setup after loading the view.
    }
    ////For Event Picker view

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

        return 1


    }

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

        if (pickerView.tag == 0) {
            //pickerView1
            return Eventstype.count
        } else if (duration.tag == 1){
            //pickerView2
            return Durations.count
        }
        return 1
        //return DurationTxt.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if (pickerView.tag == 0) {

            return Eventstype[row]
        } else if (duration.tag == 1){

           return Durations[row]
        }

        return ""
    }
        //return DurationTxt[row]


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

        if (pickerView.tag == 0) {
            Eventtype.text = Eventstype[row]
            self.view.endEditing(false)
        }else if (duration.tag == 1){
            Duration.text = Durations[row]
            self.view.endEditing(true)

        }

    }

最佳答案

如下所示在viewDidLoad中为pickerview设置标签,

pickerView.tag = 0
duration.tag = 1

关于ios - 快速在同一UIView中使用多个自定义UIPickerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43178646/

10-12 02:18