将 DatePicker 与 UIPickerView 同步,并使用 Label 显示从其中任何一个中选择的值

Synchronise DatePicker with an UIPickerView and use a Label to display the value selected from any of them

我需要一点帮助,因为我的作业很困难。 我需要将 PickerView 与 DatePicker 同步,并在标签中显示所选值。 当我更改 DatePicker 上的值时,不会反映在我的 PickerView 上。 谁能弄清楚为什么? 这是一张图片,向您展示我目前的位置以及我的代码:

导入 UIKit

class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {

    @IBOutlet weak var pickerView: UIPickerView!
    @IBOutlet weak var datePicker: UIDatePicker!
    @IBOutlet weak var labelDate: UILabel!

    var days = [String]()
    var months = Array<String>()
    var years = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        let formatter = DateFormatter()
        formatter.dateFormat = "dd/MMM/yyyy"
        labelDate.text = formatter.string(from: datePicker.date)

        // Arrays for Picker View
        days = Array(1...31).map({ String([=10=]) })
        months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
        years = Array(2000...2030).map({ String([=10=]) })
    }

    // We set number of components in the Picker View (in our case 3 columns)
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 3
    }

    // Get the number of elements for each component (each column)
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        switch(component) {
        case 0: return months.count
        case 1: return days.count
        case 2: return years.count
        default: return 0
        }
    }

    // Our ViewController will adopt the protocol "UIPickerViewDelegate" and after we display data for each element on a new Row
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {

        switch(component) {
        case 0: return months[row]
        case 1: return days[row]
        case 2: return years[row]
        default: return nil
        }
    }

      // Select a date from PickerView and show it in the DatePicker and also in the Label
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {

        let monthSelected = months[pickerView.selectedRow(inComponent: 0)]
        let daySelected = days[pickerView.selectedRow(inComponent: 1)]
        let yearSelected = years[pickerView.selectedRow(inComponent: 2)]
        let dataString = "\(daySelected)/" + "\(monthSelected)/" + "\(yearSelected)"
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "d/MMM/yyyy"
        let date = dateFormatter.date(from: dataString)
        datePicker.setDate(date!, animated: true)
        labelDate.text = "\(daySelected)/" + "\(monthSelected)/" + "\(yearSelected)"


        // Here I need the logic for February to have 28 days always or if I know
        // how to do, then to implement some Logic for Leap Years

        /*

         if(yearSelected % 4 == 0 && (yearSelected % 100 != 0) || (yearSelected % 400 == 0)){
         february will return 29 days
         } else{
         february will return 28 days }
         }

         */


    }

    // Select a date from DatePicker and show it in the UIPickerView and also in the Label
    @IBAction func dateSelectedFromDatePicker(_ sender: UIDatePicker) {

        let calendar = Calendar.current
        let components = calendar.dateComponents([.day,.month,.year], from: self.datePicker.date)
        let day = components.day
        let month = components.month
        let year = components.year

       pickerView.selectRow(month!-1, inComponent: 0, animated: true)
       pickerView.selectRow(day!-1, inComponent: 1, animated: true)
       pickerView.selectRow(year!-1, inComponent: 2, animated: true)

        let formatter = DateFormatter()
        formatter.dateFormat = "d/MMM/yyyy"
        labelDate.text = formatter.string(from: datePicker.date)

    }
}

非常感谢您!!!

组件 0 --> 月

组件 1 --> 天

组件 2 --> 年

更改此代码,确保 day!-1month!-1year!-1 代表数组日、月、年

中的正确索引

这是应该更改的代码:

    pickerView.selectRow(day!-1, inComponent: 1, animated: true)
    pickerView.selectRow(month!-1, inComponent: 0, animated: true)
    pickerView.selectRow(year!-1, inComponent: 2, animated: true)