Skip to content
Roland Moers edited this page Oct 31, 2016 · 6 revisions

Basic

Import

The first thing you need to do is importing RMDateSelectionViewController in your view controller.

Objective-C

#import "RMDateSelectionViewController.h"

Swift

import RMDateSelectionViewController

Actions

After that you need to create a select and a cancel action.

Objective-C

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");
}];

Swift

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")
}

Presentation

In the last step you create a RMDateSelectionViewController instance, set select and cancel block and present the RMDateSelectionViewController instance

Objective-C

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];

Swift

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)

Complete example

The following example combines the above code into one snippet

Objective-C

- (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];
}

Swift

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)
}

Advanced options

There are some advanced options you might want to set before presenting RMDateSelectionViewController

Date Picker

Others

There are some some more options inherited from RMActionController. These options are described in (How to user RMActionController)[https://github.com/CooperRS/RMActionController/wiki/Usage] (links to the wiki of RMActionController).

Final Note

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.

Clone this wiki locally