diff --git a/Pickers/ActionSheetDatePicker.h b/Pickers/ActionSheetDatePicker.h index 58619fab4..448ebdbe6 100755 --- a/Pickers/ActionSheetDatePicker.h +++ b/Pickers/ActionSheetDatePicker.h @@ -29,17 +29,24 @@ @class ActionSheetDatePicker; -typedef void(^ActionDateDoneBlock)(ActionSheetDatePicker *picker, NSDate *selectedDate, id origin); +typedef void(^ActionDateDoneBlock)(ActionSheetDatePicker *picker, id selectedDate, id origin); //selectedDate is NSDate or NSNumber for "UIDatePickerModeCountDownTimer" typedef void(^ActionDateCancelBlock)(ActionSheetDatePicker *picker); @interface ActionSheetDatePicker : AbstractActionSheetPicker -@property (nonatomic) NSDate *minimumDate; -@property (nonatomic) NSDate *maximumDate; -@property (nonatomic) NSInteger minuteInterval; -@property (nonatomic) NSCalendar *calendar; -@property (nonatomic) NSTimeZone *timeZone; -@property (nonatomic) NSLocale *locale; +@property (nonatomic, retain) NSDate *minimumDate; // specify min/max date range. default is nil. When min > max, the values are ignored. Ignored in countdown timer mode +@property (nonatomic, retain) NSDate *maximumDate; // default is nil + +@property (nonatomic) NSInteger minuteInterval; // display minutes wheel with interval. interval must be evenly divided into 60. default is 1. min is 1, max is 30 + +@property (nonatomic, retain) NSLocale *locale; // default is [NSLocale currentLocale]. setting nil returns to default +@property (nonatomic, copy) NSCalendar *calendar; // default is [NSCalendar currentCalendar]. setting nil returns to default +@property (nonatomic, retain) NSTimeZone *timeZone; // default is nil. use current time zone or time zone from calendar + +@property (nonatomic, assign) NSTimeInterval countDownDuration; // for UIDatePickerModeCountDownTimer, ignored otherwise. default is 0.0. limit is 23:59 (86,399 seconds). value being set is div 60 (drops remaining seconds). + +@property (nonatomic, copy) ActionDateDoneBlock onActionSheetDone; +@property (nonatomic, copy) ActionDateCancelBlock onActionSheetCancel; + (id)showPickerWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin; @@ -54,6 +61,7 @@ typedef void(^ActionDateCancelBlock)(ActionSheetDatePicker *picker); - (id)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate target:(id)target action:(SEL)action origin:(id)origin cancelAction:(SEL)cancelAction; + - (instancetype)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate @@ -63,7 +71,4 @@ typedef void(^ActionDateCancelBlock)(ActionSheetDatePicker *picker); - (void)eventForDatePicker:(id)sender; -@property (nonatomic, copy) ActionDateDoneBlock onActionSheetDone; -@property (nonatomic, copy) ActionDateCancelBlock onActionSheetCancel; - @end diff --git a/Pickers/ActionSheetDatePicker.m b/Pickers/ActionSheetDatePicker.m index 51bdc5cc6..74700b22e 100755 --- a/Pickers/ActionSheetDatePicker.m +++ b/Pickers/ActionSheetDatePicker.m @@ -32,13 +32,9 @@ @interface ActionSheetDatePicker() @property (nonatomic, assign) UIDatePickerMode datePickerMode; @property (nonatomic, strong) NSDate *selectedDate; -@property (nonatomic, assign) NSTimeInterval duration; @end @implementation ActionSheetDatePicker -@synthesize selectedDate = _selectedDate; -@synthesize datePickerMode = _datePickerMode; -@synthesize duration = _duration; + (id)showPickerWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePickerMode selectedDate:(NSDate *)selectedDate @@ -79,7 +75,6 @@ - (id)initWithTitle:(NSString *)title datePickerMode:(UIDatePickerMode)datePicke self.title = title; self.datePickerMode = datePickerMode; self.selectedDate = selectedDate; - self.duration = 60; } return self; } @@ -110,7 +105,14 @@ - (UIView *)configuredPickerView { datePicker.timeZone = self.timeZone; datePicker.locale = self.locale; - [datePicker setDate:self.selectedDate animated:NO]; + // if datepicker is set with a date in countDownMode then + // 1h is added to the initial countdown + if (self.datePickerMode == UIDatePickerModeCountDownTimer) { + datePicker.countDownDuration = self.countDownDuration; + } else { + [datePicker setDate:self.selectedDate animated:NO]; + } + [datePicker addTarget:self action:@selector(eventForDatePicker:) forControlEvents:UIControlEventValueChanged]; //need to keep a reference to the picker so we can clear the DataSource / Delegate when dismissing (not used in this picker, but just in case somebody uses this as a template for another picker) @@ -123,14 +125,19 @@ - (void)notifyTarget:(id)target didSucceedWithAction:(SEL)action origin:(id)orig { if (self.onActionSheetDone) { - self.onActionSheetDone(self, self.selectedDate, origin); + if (self.datePickerMode == UIDatePickerModeCountDownTimer) + self.onActionSheetDone(self, @(self.countDownDuration), origin); + else + self.onActionSheetDone(self, self.selectedDate, origin); + return; } else if ([target respondsToSelector:action]) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" if (self.datePickerMode == UIDatePickerModeCountDownTimer) { - [target performSelector:action withObject:@(self.duration) withObject:origin]; + [target performSelector:action withObject:@(self.countDownDuration) withObject:origin]; + } else { [target performSelector:action withObject:self.selectedDate withObject:origin]; } @@ -162,7 +169,7 @@ - (void)eventForDatePicker:(id)sender return; UIDatePicker *datePicker = (UIDatePicker *)sender; self.selectedDate = datePicker.date; - self.duration = datePicker.countDownDuration; + self.countDownDuration = datePicker.countDownDuration; } - (void)customButtonPressed:(id)sender { diff --git a/Swift-Example/Swift-Example/Base.lproj/Main.storyboard b/Swift-Example/Swift-Example/Base.lproj/Main.storyboard index 635f75ecc..f5d230c6b 100644 --- a/Swift-Example/Swift-Example/Base.lproj/Main.storyboard +++ b/Swift-Example/Swift-Example/Base.lproj/Main.storyboard @@ -1,6 +1,7 @@ + @@ -14,101 +15,172 @@ - + - + - + - - - + + - + + + + + - + - - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - - - + + - + - + - - - - - - - - - - - + + + + + + + + + + - + @@ -150,7 +222,7 @@ - + diff --git a/Swift-Example/Swift-Example/ViewControllers/SWTableViewController.swift b/Swift-Example/Swift-Example/ViewControllers/SWTableViewController.swift index 24f0bb69e..b98455401 100644 --- a/Swift-Example/Swift-Example/ViewControllers/SWTableViewController.swift +++ b/Swift-Example/Swift-Example/ViewControllers/SWTableViewController.swift @@ -11,20 +11,43 @@ import CoreActionSheetPicker class SWTableViewController: UITableViewController, UITableViewDelegate { @IBOutlet var UIDatePickerModeTime: UIButton! - @IBAction func ActionSheetDatePickerClicked(sender: AnyObject) { - var datePicker = ActionSheetDatePicker(title: "Date", datePickerMode: UIDatePickerMode.Time, selectedDate: NSDate(), target: self, action: "datePicked:", origin: self.view) + @IBAction func TimePickerClicked(sender: AnyObject) { + var datePicker = ActionSheetDatePicker(title: "Time:", datePickerMode: UIDatePickerMode.Time, selectedDate: NSDate(), target: self, action: "datePicked:", origin: self.view) datePicker.minuteInterval = 20 - datePicker.showActionSheetPicker() } + @IBAction func DatePickerClicked(sender: AnyObject) { + var datePicker = ActionSheetDatePicker(title: "Date:", datePickerMode: UIDatePickerMode.Date, selectedDate: NSDate(), doneBlock: {ActionStringDoneBlock in return}, cancelBlock: {ActionStringCancelBlock in return }, origin: self.view) + let secondsInWeek: NSTimeInterval = 7 * 24 * 60 * 60; + datePicker.minimumDate = NSDate(timeInterval: -secondsInWeek, sinceDate: NSDate()) + datePicker.maximumDate = NSDate(timeInterval: secondsInWeek, sinceDate: NSDate()) + + datePicker.showActionSheetPicker() + } + + @IBAction func DateAndTimeClicked(sender: AnyObject) { + var datePicker = ActionSheetDatePicker(title: "DateAndTime:", datePickerMode: UIDatePickerMode.DateAndTime, selectedDate: NSDate(), doneBlock: {ActionStringDoneBlock in return}, cancelBlock: {ActionStringCancelBlock in return }, origin: self.view) + let secondsInWeek: NSTimeInterval = 7 * 24 * 60 * 60; + datePicker.minimumDate = NSDate(timeInterval: -secondsInWeek, sinceDate: NSDate()) + datePicker.maximumDate = NSDate(timeInterval: secondsInWeek, sinceDate: NSDate()) + datePicker.minuteInterval = 20 + + datePicker.showActionSheetPicker() + } + @IBAction func CountdownTimerClicked(sender: AnyObject) { + var datePicker = ActionSheetDatePicker(title: "CountDownTimer:", datePickerMode: UIDatePickerMode.CountDownTimer, selectedDate: NSDate(), doneBlock: {ActionStringDoneBlock in return}, cancelBlock: {ActionStringCancelBlock in return }, origin: self.view) + + datePicker.countDownDuration = 60 * 7 + datePicker.showActionSheetPicker() + } @IBAction func navigationItemPicker(sender: AnyObject) { - ActionSheetStringPicker.showPickerWithTitle("Nav Bar From Picker", rows: ["One", "Two", "A lot"], initialSelection: 1, doneBlock: nil, cancelBlock: nil, origin: self.view) + ActionSheetStringPicker.showPickerWithTitle("Nav Bar From Picker", rows: ["One", "Two", "A lot"], initialSelection: 1, doneBlock: {ActionStringDoneBlock in return}, cancelBlock: {ActionStringCancelBlock in return }, origin: self.view) } @IBAction func localePickerClicked(sender: AnyObject) { - ActionSheetLocalePicker.showPickerWithTitle("Locale picker", initialSelection: NSTimeZone(), doneBlock: nil, cancelBlock: nil, origin: self.view) + ActionSheetLocalePicker.showPickerWithTitle("Locale picker", initialSelection: NSTimeZone(), doneBlock: {ActionStringDoneBlock in return}, cancelBlock: {ActionStringCancelBlock in return }, origin: self.view) } @IBOutlet var localePicker: UIButton!