如何在单个视图控制器中创建多个pickerView?

时间:2019-03-02 11:12:43

标签: ios swift swift4.2

@IBOutlet weak var searchMonth: UITextField!

@IBOutlet weak var searchDate: UITextField!

var months =["January","Febrary","March","April","May","June","July","August","September","October","November","December"]

var year = ["2017","2018","2019"]

let thePicker = UIPickerView()

    let thePicker2 = UIPickerView()

    override func viewDidLoad() {
        super.viewDidLoad()

        searchMonth.inputView = thePicker
        thePicker.delegate = self

        searchMonth.inputView = thePicker2
        thePicker2.delegate = self


    }


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

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

        if searchMonth.tag == 1{
             return months.count
        }
        else if searchDate.tag == 2{

            return year.count
        }
        else {
            return 0
        }


    }

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

        if searchMonth.tag == 1 {
             return months[row]
        }
        else if searchDate.tag == 2{
           return year[row]
        } else{
              return ""
        }

        }

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

        if searchMonth.tag == 1  {
             searchMonth.text = months[row]
        }
        else if searchDate.tag == 2 {
            searchDate.text = year[row]
        }


    }

2 个答案:

答案 0 :(得分:1)

首先要将选择器分配给视图中加载的同一文本字段searchMonth

您应该这样命名您的选择器,以便更好地理解

let monthPicker = UIPickerView()
let datePicker = UIPickerView()

第二,您只需要用适当的选择器替换您的条件,您的方法将如下所示:-

@IBOutlet weak var searchMonth: UITextField!
@IBOutlet weak var searchDate: UITextField!

var months =["January","Febrary","March","April","May","June","July","August","September","October","November","December"]
var year = ["2017","2018","2019"]

let monthPicker = UIPickerView()
let datePicker = UIPickerView()


override func viewDidLoad() {
    super.viewDidLoad()
    searchMonth.inputView = monthPicker
    monthPicker.delegate = self
    monthPicker.dataSource = self
    searchDate.inputView = datePicker
    datePicker.delegate = self
    datePicker.dataSource = self
}

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if pickerView == monthPicker {
        return months.count
    }else if pickerView == datePicker {
        return year.count
    }else {
        return 0
    }
}

func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == monthPicker {
        return months[row]
    }else if pickerView == datePicker {
        return year[row]
    }else {
        return ""
    }
}

func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == monthPicker {
        searchMonth.text = months[row]
    }else if pickerView == datePicker {
        searchDate.text = year[row]
    }
}

如果在视图控制器中仅使用两个选择器,则可以通过使用以下简单条件来减小代码的大小:-

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    return pickerView == monthPicker ? months.count : year.count
}

答案 1 :(得分:0)

SearchResult类:UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate {

@IBOutlet weak var searchMonth: UITextField!
@IBOutlet weak var searchDate: UITextField!

var months = ["January","Febrary","March","April","May","June","July","August","September","October","November","December"]
var year = ["2017","2018","2019"]
let monthPicker = UIPickerView()
let datePicker = UIPickerView()

override func viewDidLoad() {
    super.viewDidLoad()

    searchMonth.inputView = monthPicker
    searchMonth.delegate = self

    searchDate.inputView = datePicker
    searchDate.delegate = self
    monthPicker.dataSource = self
    datePicker.dataSource = self

}

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

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

    if pickerView == monthPicker {
        return months.count
    }else if pickerView == datePicker {
        return year.count
    }else {
        return 0
    }
}

func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if pickerView == monthPicker {
        return months[row]
    }else if pickerView == datePicker {
        return year[row]
    }else {
        return ""
    }
}

func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    if pickerView == monthPicker {
        searchMonth.text = months[row]
    }else if pickerView == datePicker {
        searchDate.text = year[row]
    }
}

//Not showing data