在swift中的同一个UIView中有多个自定义UIPickerView

时间:2017-04-03 07:15:40

标签: ios swift uipickerview

我试图在同一个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)

        }

    }

3 个答案:

答案 0 :(得分:1)

 @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
        pickerView.tag = 0
        duration.tag = 1
        // 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)

        }

    }

这是通过标签完成的,否则你可以检查代理

if pickerView == duration

答案 1 :(得分:0)

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

pickerView.tag = 0
duration.tag = 1

答案 2 :(得分:0)

class ViewController: UIViewController {

    @IBOutlet var txtDuration: UITextField!
    @IBOutlet var txtEventType: UITextField!

    var pickerView = UIPickerView()

    var arrEventType : [String] = []
    var arrDuration : [String] = []

    var currentTextField : UITextField?

    override func viewDidLoad() {
        super.viewDidLoad()

        arrEventType = ["Birthday" , "Marriage" , "Get together", "Conference"]
        arrDuration  = ["6-8","1-5","7-9","10-12"]
    }
}

extension ViewController : UIPickerViewDelegate , UIPickerViewDataSource {

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

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

        if currentTextField == txtEventType {
            return arrEventType.count
        } else if currentTextField == txtDuration {
            return arrDuration.count
        } else {
            return 0
        }
    }

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

        if currentTextField == txtEventType {
            return arrEventType[row]
        } else if currentTextField == txtDuration {
            return arrDuration[row]
        } else {
            return ""
        }
    }

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

        if currentTextField == txtEventType {
            txtEventType.text = arrEventType[row]
        } else if currentTextField == txtDuration {
            txtDuration.text = arrDuration[row]
        }
    }
}

extension ViewController : UITextFieldDelegate {

    func textFieldDidBeginEditing(_ textField: UITextField) {
        self.pickerView.delegate   = self
        self.pickerView.dataSource = self
        currentTextField = textField
        if currentTextField == txtEventType {
            currentTextField?.inputView = pickerView
        } else if currentTextField == txtDuration {
            currentTextField?.inputView  = pickerView
        }

    }
}

上面的代码行可以解决您的问题只有一个UIPickerView而没有标记