UIDatePicker - 日期更改

时间:2010-09-24 09:05:02

标签: iphone objective-c uidatepicker

我刚开始在我的iPad应用中使用UIDatePicker,但有没有办法检查日期何时更改?

我想在日期改变时做点什么。希望你能帮忙,谢谢。

4 个答案:

答案 0 :(得分:74)

  

正确配置后,a   UIDatePicker对象发送一个动作   用户完成旋转时的消息   改变日期的其中一个轮子   或时间;相关的控制事件   是UIControlEventValueChanged。

所以你需要添加你的类来处理你的选择器中的UIControlEventValueChanged事件:

[picker addTarget:self action:@selector(dateChanged:) 
              forControlEvents:UIControlEventValueChanged];
...

- (void) dateChanged:(id)sender{
   // handle date changes
}

答案 1 :(得分:9)

Swift 4版本:

UIDatePicker设置:

    let datePicker = UIDatePicker()
    datePicker.datePickerMode = .date
    dateTextField.inputView = datePicker
    datePicker.addTarget(self, action: #selector(datePickerChanged(picker:)), for: .valueChanged)

功能:

@objc func datePickerChanged(picker: UIDatePicker) {
    print(picker.date)
}

如果您注意到,如果您要使用 #selector()

,则Swift 4在函数中需要 @objc

答案 2 :(得分:2)

Swift 3版本:

override func viewDidLoad() {
    super.viewDidLoad()

    if sYourSelectedDate == "" {
        if let plist = Plist(name: "YourPlistName") {
            sYourSelectedDate = plist.getSingleValueInPlistFile(sFieldName: "yourSelectedDate")!

        }
    }
    if sYourSelectedDateTime == "" {
        if let plist = Plist(name: "YourPlistName") {
            sYourSelectedDateTime = plist.getSingleValueInPlistFile(sFieldName: "yourSelectedDateTime")!

        }
    }

    // add dateTimePickerChanged event (e.g., if device is powered down, values get saved)
    self.dateTimePicker.addTarget(self, action: #selector(dateTimeChanged), for: .valueChanged);

    // set min and max dates of datetimePicker
    // subtracting 1 minute from current time
    let currentTempDate = NSDate()
    let newDate = currentTempDate.addingTimeInterval(-1 * 60);
    let currentDate = newDate;

    let calendar: NSCalendar = NSCalendar(calendarIdentifier: NSCalendar.Identifier.gregorian)!

    calendar.timeZone = NSTimeZone(name: "US/Eastern")! as TimeZone

    let components: NSDateComponents = NSDateComponents()
    components.calendar = calendar as Calendar

    components.year = -1
    let minDate: NSDate = calendar.date(byAdding: components as DateComponents, to: currentDate as Date, options: NSCalendar.Options(rawValue: 0))! as NSDate

    let maxDate: NSDate = currentDate as NSDate

    self.dateTimePicker.minimumDate = minDate as Date
    self.dateTimePicker.maximumDate = maxDate as Date

    //print("minDate: \(minDate)")
    //print("maxDate: \(maxDate)")

    if(sYourSelectedDateTime == "")
    {
        // subtracting 1 minute from current time
        let currentTempDate = NSDate()
        let newDate = currentTempDate.addingTimeInterval(-1 * 60);
        let currentDate2 = newDate;

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat =  "MM/dd/yyyy h:mm a"

        calendar.timeZone = NSTimeZone(name: "US/Eastern")! as TimeZone

        let userCalendar = Calendar.current
        let requestedComponents: Set<Calendar.Component> = [
            .year,
            .month,
            .day,
            .hour,
            .minute
        ]

        let dateTimeComponents = userCalendar.dateComponents(requestedComponents, from: currentDate2 as Date)

        let iPhoneDateComponents = NSDateComponents()
        iPhoneDateComponents.year = dateTimeComponents.year!;
        iPhoneDateComponents.month = dateTimeComponents.month!;
        iPhoneDateComponents.day = dateTimeComponents.day!;
        iPhoneDateComponents.hour = dateTimeComponents.hour!;
        iPhoneDateComponents.minute = dateTimeComponents.minute!;
        iPhoneDateComponents.timeZone = NSTimeZone(name: "US/Eastern") as TimeZone?

        let iPhoneDate = calendar.date(from: iPhoneDateComponents as DateComponents)!

        self.dateTimePicker.date = iPhoneDate;
        sYourInitialDateTime = dateFormatter.string(from: self.dateTimePicker.date);

        if let plist = Plist(name: "YourPlistFile") {
            plist.editSingleValueInPlistFile(dictString: "YourPlistFile", sFieldName: "yourInitialDateTime", sValue: sYourInitialDateTime)
        }

    } else {
        let dateFormatter: DateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM/dd/yyyy h:mm a"
        let dateObj = dateFormatter.date(from: sYourSelectedDateTime)
        self.dateTimePicker.date = dateObj!
    }

}

func dateTimeChanged(picker: UIDatePicker) {
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat =  "MM/dd/yyyy"
    sYourSelectedDate = dateFormatter.string(from: picker.date);
    dateFormatter.dateFormat =  "MM/dd/yyyy h:mm a"

    sYourSelectedDateTime = dateFormatter.string(from: picker.date);

    if (sYourSelectedDateTime == sYourInitialDateTime)
    {
        sYourSelectedDate = "";
        sYourSelectedDateTime = "";
    }

    if let plist = Plist(name: "YourPlistFile") {
        plist.editSingleValueInPlistFile(dictString: "YourPlistName", sFieldName: "yourSelectedDate", sValue: sYourSelectedDate)
        plist.editSingleValueInPlistFile(dictString: "YourPlistName", sFieldName: "yourSelectedDateTime", sValue: sYourSelectedDateTime)

    } else {
        print("Unable to get Plist")
    }
    print(sYourSelectedDate);
    print(sYourSelectedDateTime);

}

答案 3 :(得分:0)

swift version

override func viewDidLoad() {
    super.viewDidLoad()
    let picker = UIDatePicker(frame: CGRectMake(0, 0, 200, 200))
    picker.addTarget(self, action: #selector(datePickerChanged(_:)), forControlEvents: .ValueChanged)
    view.addSubview(picker)
}

func datePickerChanged(picker: UIDatePicker) {
    print(picker.date)
}