Skip to content
Roland Moers edited this page Nov 20, 2016 · 2 revisions

Basic

Import

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

Objective-C

#import <RMPickerViewController/RMPickerViewController.h>

Swift

import RMPickerViewController

Actions

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

Objective-C

RMAction<UIPickerView *> *selectAction = [RMAction<UIPickerView *> actionWithTitle:@"Select" style:RMActionStyleDone andHandler:^(RMActionController<UIPickerView *> *controller) {
    NSMutableArray *selectedRows = [NSMutableArray array];
    
    for(NSInteger i=0 ; i<[controller.contentView numberOfComponents] ; i++) {
        [selectedRows addObject:@([controller.contentView selectedRowInComponent:i])];
    }
    
    NSLog(@"Successfully selected rows: %@", selectedRows);
}];

RMAction<UIPickerView *> *cancelAction = [RMAction<UIPickerView *> actionWithTitle:@"Cancel" style:RMActionStyleCancel andHandler:^(RMActionController<UIPickerView *> *controller) {
    NSLog(@"Row selection was canceled");
}];

Swift

let selectAction = RMAction<UIPickerView>(title: "Select", style: .done) { controller in
    let selectedRows = NSMutableArray();
    for i in 0 ..< controller.contentView.numberOfComponents {
        selectedRows.add(controller.contentView.selectedRow(inComponent: i));
    }
    print("Successfully selected rows: ", selectedRows);
}

let cancelAction = RMAction<UIPickerView>(title: "Cancel", style: .cancel) { _ in
    print("Row selection was canceled")
}

Presentation

In the last step you create a RMPickerViewController instance, set select and cancel block and present the RMPickerViewController instance. Do not forget to set the delegate and data source for the picker view.

Objective-C

RMPickerViewController *pickerController = [RMPickerViewController actionControllerWithStyle:style title:@"Test" message:@"This is a test message.\nPlease choose a row and press 'Select' or 'Cancel'." selectAction:selectAction andCancelAction:cancelAction];
pickerController.picker.dataSource = self;
pickerController.picker.delegate = self;

//Now just present the picker controller using the standard iOS presentation method
[self presentViewController:pickerController animated:YES completion:nil];

Swift

let actionController = RMPickerViewController(style: style, title: "Test", message: "This is a test message.\nPlease choose a row and press 'Select' or 'Cancel'.", select: selectAction, andCancel: cancelAction)!;
actionController.picker.delegate = self;
actionController.picker.dataSource = self;

//Now just present the picker 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)openPickerController:(id)sender {
    RMAction<UIPickerView *> *selectAction = [RMAction<UIPickerView *> actionWithTitle:@"Select" style:RMActionStyleDone andHandler:^(RMActionController<UIPickerView *> *controller) {
        NSMutableArray *selectedRows = [NSMutableArray array];
    
        for(NSInteger i=0 ; i<[controller.contentView numberOfComponents] ; i++) {
            [selectedRows addObject:@([controller.contentView selectedRowInComponent:i])];
        }
        
        NSLog(@"Successfully selected rows: %@", selectedRows);
    }];
    
    RMAction<UIPickerView *> *cancelAction = [RMAction<UIPickerView *> actionWithTitle:@"Cancel" style:RMActionStyleCancel andHandler:^(RMActionController<UIPickerView *> *controller) {
        NSLog(@"Row selection was canceled");
    }];
    
    RMPickerViewController *pickerController = [RMPickerViewController actionControllerWithStyle:style title:@"Test" message:@"This is a test message.\nPlease choose a row and press 'Select' or 'Cancel'." selectAction:selectAction andCancelAction:cancelAction];
    pickerController.picker.dataSource = self;
    pickerController.picker.delegate = self;
    
    //Now just present the picker controller using the standard iOS presentation method
    [self presentViewController:pickerController animated:YES completion:nil];
}

Swift

func openPickerViewController() {
    let selectAction = RMAction<UIPickerView>(title: "Select", style: .done) { controller in
    let selectedRows = NSMutableArray();
    for i in 0 ..< controller.contentView.numberOfComponents {
            selectedRows.add(controller.contentView.selectedRow(inComponent: i));
        }
        print("Successfully selected rows: ", selectedRows);
    }

    let cancelAction = RMAction<UIPickerView>(title: "Cancel", style: .cancel) { _ in
        print("Row selection was canceled")
    }
    
    let actionController = RMPickerViewController(style: style, title: "Test", message: "This is a test message.\nPlease choose a row and press 'Select' or 'Cancel'.", select: selectAction, andCancel: cancelAction)!;
    actionController.picker.delegate = self;
    actionController.picker.dataSource = self;

    //Now just present the picker 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 RMPickerViewController.

Picker View

RMPickerViewController has a property called picker. With this property you have total control over the UIPickerView that is shown on the screen. For example, you need this property to set delegate and data source for the UIPickerView.

Additional Actions

You can add an arbitrary number of custom buttons to a RMPickerViewController. Each button has it's own block that is executed when tapping the button. See the following example on how to add buttons.

Objective-C

- (IBAction)openPickerViewController:(id)sender {
    // Code as shown above
    ...
    
    RMPickerViewController *pickerController = [RMPickerViewController actionControllerWithStyle:style title:@"Test" message:@"This is a test message.\nPlease choose a row and press 'Select' or 'Cancel'." selectAction:selectAction andCancelAction:cancelAction];
    pickerController.picker.dataSource = self;
    pickerController.picker.delegate = self;

    // Add your additional action
    RMAction<UIPickerView *> *additionalAction = [RMAction<UIPickerView *> actionWithTitle:@"Action" style:RMActionStyleAdditional andHandler:^(RMActionController<UIPickerView *> * _Nonnull controller) {
        ..
    }];
    additionalAction.dismissesActionController = NO;
    
    [pickerController addAction:additionalAction];

    //Now just present the picker controller using the standard iOS presentation method
    [self presentViewController:pickerController animated:YES completion:nil];
}

Swift

func openPickerViewController() {
    // Code as shown above
    ...

    let actionController = RMPickerViewController(style: style, title: "Test", message: "This is a test message.\nPlease choose a row and press 'Select' or 'Cancel'.", select: selectAction, andCancel: cancelAction)!;
    actionController.picker.delegate = self;
    actionController.picker.dataSource = self;

    // Add your additional action
    let pickerAction = RMAction<UIPickerView>(title: "Now", style: .additional) { controller -> Void in
        ...
    }
    pickerAction!.dismissesActionController = false;
        
    actionController.addAction(pickerAction!);

    //Now just present the picker controller using the standard iOS presentation method
    present(actionController, animated: true, completion: nil)
}

Others

There are some some more options inherited from RMActionController. These options are described in How to use RMActionController (links to the wiki of RMActionController).

Final Note

As with RMActionController, you may use RMPickerViewController in both your main application and your action extension showing an user interface. RMPickerViewController only uses APIs that are safe to be used in extensions, too.