第二个日期选择器应显示从第一个日期选择器中选择的日期之后的日期

时间:2018-01-03 04:31:21

标签: ios swift swift3

我有两个日期选择器。首先我选择一个日期,我希望第二个日期选择器显示选定的第一个选择日期之后的日期。应禁用以前的日期。

     else if textFieldType == .RequestFromDate
            {
                addToolBar()
                requestFromdatePicker.datePickerMode = .date
                textField.inputView = requestFromdatePicker
                requestFromdatePicker.minimumDate = Date()
                requestFromdatePicker.addTarget(self, action: #selector(datePickerRequestFromDateClicked(sender:)), for: .valueChanged)

            }

 else if textFieldType == .RequestToDate
        {
            addToolBar()
            requestTodatePicker.datePickerMode = .date
            textField.inputView = requestTodatePicker
            requestTodatePicker.minimumDate = requestFromdatePicker.date
            requestTodatePicker.date = requestFromdatePicker.date
            requestTodatePicker.addTarget(self, action: #selector(datePickerRequestToDateClicked(sender:)), for: .valueChanged)
        }

1 个答案:

答案 0 :(得分:0)

在这里你可以这样做

import UIKit

class ViewController: UIViewController {

//label for first date picker result
@IBOutlet weak var dateLabel: UILabel!

//first date picker
@IBOutlet weak var datePicker: UIDatePicker!

//secomnd date picker
@IBOutlet weak var secondDatePicker: UIDatePicker!

//Action - Value changed for first date picker
@IBAction func datePickerChanged(_ sender: Any) {
        let dateFormatter = DateFormatter()

        dateFormatter.dateStyle = DateFormatter.Style.short
        dateFormatter.timeStyle = DateFormatter.Style.short

    //here you are getting date as its action for value changed
        let strDate = dateFormatter.string(from: datePicker.date)
        dateLabel.text = strDate

    //here you can just update second date picker date when value of first date picker is changed
        secondDatePicker.date = datePicker.date

    //set the minimum date so past dates dont be selected
    //even if user scroll to past dates they wont be selected
    //picker wil automatically come to minimum date
    secondDatePicker.minimumDate = datePicker.date
}

override func viewDidLoad() {
    super.viewDidLoad()
    // 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.
}


}

视频链接 - https://drive.google.com/open?id=1O3avb_n-CPF_7aLat9oX-nzV7k5KklFe