Skip to content
Petr Korolev edited this page May 4, 2015 · 5 revisions

###Basic Usage

// Inside a IBAction method:

// Create an array of strings you want to show in the picker:
NSArray *colors = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", @"Orange", nil];

[ActionSheetStringPicker showPickerWithTitle:@"Select a Color"
                                        rows:colors
                            initialSelection:0
                                   doneBlock:^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
                                      NSLog(@"Picker: %@", picker);
                                      NSLog(@"Selected Index: %@", selectedIndex);
                                      NSLog(@"Selected Value: %@", selectedValue);
                                    }
                                 cancelBlock:^(ActionSheetStringPicker *picker) {
                                      NSLog(@"Block Picker Canceled");
                                    }
                                      origin:sender];
// You can also use self.view if you don't have a sender

ActionSheetCustomPicker Customisation

ActionSheetCustomPicker provides the following delegate function that can be used for customisation:

- (void)actionSheetPicker:(AbstractActionSheetPicker *)actionSheetPicker configurePickerView:(UIPickerView *)pickerView;

This method is called right before actionSheetPicker is presented and it can be used to customize the appearance and properties of the actionSheetPicker and the pickerView associated with it.

Want custom buttons view? Ok!

Example with custom text in Done button:

    ActionSheetStringPicker *picker = [[ActionSheetStringPicker alloc] initWithTitle:@"Select a Block" rows:colors initialSelection:0 doneBlock:done cancelBlock:cancel origin:sender];
    [picker setDoneButton:[[UIBarButtonItem alloc] initWithTitle:@"My Text"  style:UIBarButtonItemStylePlain target:nil action:nil]];
    [picker showActionSheetPicker];

Example with custom button for cancel button:

    ActionSheetStringPicker *picker = [[ActionSheetStringPicker alloc] initWithTitle:@"Select a Block" rows:colors initialSelection:0 doneBlock:done cancelBlock:cancel origin:sender];
    UIButton *cancelButton =  [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setImage:[UIImage imageNamed:@"cancel.png"] forState:UIControlStateNormal];
    [cancelButton setFrame:CGRectMake(0, 0, 32, 32)];
    [picker setCancelButton:[[UIBarButtonItem alloc] initWithCustomView:cancelButton]];
    [picker showActionSheetPicker];

What about custom buttons callbacks? Let's check it out:

 // Inside a IBAction method:

 // Create an array of strings you want to show in the picker:
NSArray *colors = [NSArray arrayWithObjects:@"Red", @"Green", @"Blue", @"Orange", nil];
 
 //Create your picker:
ActionSheetStringPicker *colorPicker = [[ActionSheetStringPicker alloc] initWithTitle:@"Select a color"
                                                                                 rows:colors
                                                                     initialSelection:0
                                                                               target:nil
                                                                        successAction:nil
                                                                         cancelAction:nil
                                                                               origin:sender];
 
 //You can pass your picker a value on custom button being pressed:
[colorPicker addCustomButtonWithTitle:@"Value" value:@([colors indexOfObject:colors.lastObject])];
 
 //Or you can pass it custom block:
[colorPicker addCustomButtonWithTitle:@"Block" actionBlock:^{
    NSLog(@"Custom block invoked");
}];

 //If you prefer to send selectors rather than blocks you can use this method:
[colorPicker addCustomButtonWithTitle:@"Selector" target:self selector:@selector(awesomeSelector)];

Action by clicking outside of the picker:

Use property tapDismissAction to specify action, by clicking outside area of the picker:

  • TapActionNone (default)
  • TapActionSuccess
  • TapActionCancel

Other customisations:

look at AbstractActionSheetPicker properties:

  • toolbar
  • title
  • pickerView
  • viewSize
  • customButtons
  • hideCancel - show or hide cancel button.
  • titleTextAttributes - default is nil. Used to specify Title Label attributes.
  • attributedTitle - default is nil. If titleTextAttributes not nil this value ignored.
  • popoverBackgroundViewClass -allow popover customization on iPad
  • supportedInterfaceOrientations - You can set your own supportedInterfaceOrientations value to prevent dismissing picker in some special cases.
  • tapDismissAction - to specify action, by clicking outside area of the picker
    • TapActionNone
    • TapActionSuccess
    • TapActionCancel