-
Notifications
You must be signed in to change notification settings - Fork 167
Usage
The first thing you need to do is importing RMDateSelectionViewController in your view controller.
#import <RMDateSelectionViewController/RMDateSelectionViewController.h>
import RMDateSelectionViewController
After that you need to create a select and a cancel action.
RMAction<UIDatePicker *> *selectAction = [RMAction<UIDatePicker *> actionWithTitle:@"Select" style:RMActionStyleDone andHandler:^(RMActionController<UIDatePicker *> *controller) {
NSLog(@"Successfully selected date: %@", controller.contentView.date);
}];
RMAction<UIDatePicker *> *cancelAction = [RMAction<UIDatePicker *> actionWithTitle:@"Cancel" style:RMActionStyleCancel andHandler:^(RMActionController<UIDatePicker *> *controller) {
NSLog(@"Date selection was canceled");
}];
let selectAction = RMAction<UIDatePicker>(title: "Select", style: RMActionStyle.done) { controller in
print("Successfully selected date: ", controller.contentView.date);
}
let cancelAction = RMAction<UIDatePicker>(title: "Cancel", style: RMActionStyle.cancel) { _ in
print("Date selection was canceled")
}
In the last step you create a RMDateSelectionViewController instance, set select and cancel block and present the RMDateSelectionViewController instance
RMDateSelectionViewController *dateSelectionController = [RMDateSelectionViewController actionControllerWithStyle:RMActionControllerStyleWhite title:@"Test" message:@"This is a test message.\nPlease choose a date and press 'Select' or 'Cancel'." selectAction:selectAction andCancelAction:cancelAction];
//Now just present the date selection controller using the standard iOS presentation method
[self presentViewController:dateSelectionController animated:YES completion:nil];
let actionController = RMDateSelectionViewController(style: .white, title: "Test", message: "This is a test message.\nPlease choose a date and press 'Select' or 'Cancel'.", select: selectAction, andCancel: cancelAction)!;
//Now just present the date selection controller using the standard iOS presentation method
present(actionController, animated: true, completion: nil)
The following example combines the above code into one snippet
- (IBAction)openDateSelectionController:(id)sender {
RMAction<UIDatePicker *> *selectAction = [RMAction<UIDatePicker *> actionWithTitle:@"Select" style:RMActionStyleDone andHandler:^(RMActionController<UIDatePicker *> *controller) {
NSLog(@"Successfully selected date: %@", controller.contentView.date);
}];
RMAction<UIDatePicker *> *cancelAction = [RMAction<UIDatePicker *> actionWithTitle:@"Cancel" style:RMActionStyleCancel andHandler:^(RMActionController<UIDatePicker *> *controller) {
NSLog(@"Date selection was canceled");
}];
RMDateSelectionViewController *dateSelectionController = [RMDateSelectionViewController actionControllerWithStyle:RMActionControllerStyleWhite title:@"Test" message:@"This is a test message.\nPlease choose a date and press 'Select' or 'Cancel'." selectAction:selectAction andCancelAction:cancelAction];
//Now just present the date selection controller using the standard iOS presentation method
[self presentViewController:dateSelectionController animated:YES completion:nil];
}
func openDateSelectionViewController() {
let selectAction = RMAction<UIDatePicker>(title: "Select", style: RMActionStyle.done) { controller in
print("Successfully selected date: ", controller.contentView.date);
}
let cancelAction = RMAction<UIDatePicker>(title: "Cancel", style: RMActionStyle.cancel) { _ in
print("Date selection was canceled")
}
let actionController = RMDateSelectionViewController(style: .white, title: "Test", message: "This is a test message.\nPlease choose a date and press 'Select' or 'Cancel'.", select: selectAction, andCancel: cancelAction)!;
//Now just present the date selection controller using the standard iOS presentation method
present(actionController, animated: true, completion: nil)
}
There are some advanced options you might want to set before presenting RMDateSelectionViewController
RMDateSelectionViewController has a property called datePicker. With this property you have total control over the UIDatePicker that is shown on the screen. For example, if you want to select date and time with a 5 minute interval and the current date and time preselected, this might look as follows:
- (IBAction)openDateSelectionController:(id)sender {
// Code as shown above
...
RMDateSelectionViewController *dateSelectionController = [RMDateSelectionViewController actionControllerWithStyle:RMActionControllerStyleWhite title:@"Test" message:@"This is a test message.\nPlease choose a date and press 'Select' or 'Cancel'." selectAction:selectAction andCancelAction:cancelAction];
//You can access the actual UIDatePicker via the datePicker property
dateSelectionController.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
dateSelectionController.datePicker.minuteInterval = 5;
dateSelectionController.datePicker.date = [NSDate dateWithTimeIntervalSinceReferenceDate:0];
//Now just present the date selection controller using the standard iOS presentation method
[self presentViewController:dateSelectionController animated:YES completion:nil];
}
func openDateSelectionViewController() {
// Code as shown above
...
let actionController = RMDateSelectionViewController(style: .white, title: "Test", message: "This is a test message.\nPlease choose a date and press 'Select' or 'Cancel'.", select: selectAction, andCancel: cancelAction)!;
//You can access the actual UIDatePicker via the datePicker property
actionController.datePicker.datePickerMode = .dateAndTime;
actionController.datePicker.minuteInterval = 5;
actionController.datePicker.date = Date(timeIntervalSinceReferenceDate: 0);
//Now just present the date selection controller using the standard iOS presentation method
present(actionController, animated: true, completion: nil)
}
You can add an arbitrary number of custom buttons to a RMDateSelectionViewController. Each button has it's own block that is executed when tapping the button. See the following example on how to add buttons.
- (IBAction)openDateSelectionController:(id)sender {
// Code as shown above
...
RMDateSelectionViewController *dateSelectionController = [RMDateSelectionViewController actionControllerWithStyle:RMActionControllerStyleWhite title:@"Test" message:@"This is a test message.\nPlease choose a date and press 'Select' or 'Cancel'." selectAction:selectAction andCancelAction:cancelAction];
// Add your additional action
RMAction<UIDatePicker *> *nowAction = [RMAction<UIDatePicker *> actionWithTitle:@"Now" style:RMActionStyleAdditional andHandler:^(RMActionController<UIDatePicker *> * _Nonnull controller) {
controller.contentView.date = [NSDate date];
}];
nowAction.dismissesActionController = NO;
[dateSelectionController addAction:nowAction];
//Now just present the date selection controller using the standard iOS presentation method
[self presentViewController:dateSelectionController animated:YES completion:nil];
}
func openDateSelectionViewController() {
// Code as shown above
...
let actionController = RMDateSelectionViewController(style: .white, title: "Test", message: "This is a test message.\nPlease choose a date and press 'Select' or 'Cancel'.", select: selectAction, andCancel: cancelAction)!;
// Add your additional action
let nowAction = RMAction<UIDatePicker>(title: "Now", style: .additional) { controller -> Void in
controller.contentView.date = Date();
}
nowAction!.dismissesActionController = false;
actionController.addAction(nowAction!);
//Now just present the date selection controller using the standard iOS presentation method
present(actionController, animated: true, completion: nil)
}
There are some some more options inherited from RMActionController. These options are described in How to use RMActionController (links to the wiki of RMActionController).
As with RMActionController, you may use RMDateSelectionViewController in both your main application and your action extension showing an user interface. RMDateSelectionViewController only uses APIs that are safe to be used in extensions, too.