-
Notifications
You must be signed in to change notification settings - Fork 51
Usage
The first thing you need to do is importing RMPickerViewController in your view controller.
#import <RMPickerViewController/RMPickerViewController.h>
import RMPickerViewController
After that you need to create a select and a cancel action.
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");
}];
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")
}
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.
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];
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)
The following example combines the above code into one snippet
- (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];
}
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)
}
There are some advanced options you might want to set before presenting RMPickerViewController.
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.
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.
- (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];
}
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)
}
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 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.