Skip to content

Instantly share code, notes, and snippets.

@enriquez
Forked from anonymous/gist:4355052
Last active December 10, 2015 00:59
Show Gist options
  • Save enriquez/4355068 to your computer and use it in GitHub Desktop.
Save enriquez/4355068 to your computer and use it in GitHub Desktop.
// Example of a better API for UIActionSheet
- (void)showActionSheet {
ECActionSheet *actionSheet = [[ECActionSheet alloc] initWithTitle:@"Title" cancelButtonTitle:@"Cancel"];
// set a destructive button if needed
[actionSheet addDestructiveButtonWithTitle:@"Destructive Button" target:self action:@selector(destructiveButtonPressed)];
// add buttons with target/action
[actionSheet addButtonWithTitle:@"Button One" target:self action:@selector(buttonOnePressed)];
id someObject = something;
[actionSheet addButtonWithTitle:@"Button Two" target:self action:@selector(buttonTwoPressed) withObject:someObject];
// or add buttons with a block
[actionSheet addButtonWithTitle:@"Button Three" onTapped:^{
// do something when button three is tapped
}];
[actionSheet showInView:self.view];
}
- (void)destructiveButtonPressed {
// do something when destructive button is tapped
}
- (void)buttonOnePressed {
// do something when button one is tapped
}
- (void)buttonTwoPressed:(id)someObject {
// do something with someObject when button two is tapped
}
// Example of an equivalent UIActionSheet API
@property (nonatomic, strong) id someObject;
- (void)showActionSheet {
self.someObject = something; // set property to access it later
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Button One", @"Button Two", @"Button Three", nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *clickedButtonTitle = [actionSheet buttonTitleAtIndex:buttonIndex];
if ([clickedButtonTitle isEqualToString:@"Destructive Button"]) {
// do something when destructive button is tapped
} else if ([clickedButtonTitle isEqualToString:@"Button One"]) {
// do something when button one is tapped
} else if ([clickedButtonTitle isEqualToString:@"Button Two"]) {
// do something with self.someObject when button two is tapped
} else if ([clickedButtonTitle isEqualToString:@"Button Three"]) {
// do something when button three is tapped
}
}
@enriquez
Copy link
Author

It would be nice if the UIActionSheet constructor initWithTitle:delegate:cancelButtonTitle:destructiveButtonTitle:otherButtonTitles: was still supported so that it could be a drop in replacement.

This could just be a simple subclass with additional features.

@felixflores
Copy link

I think this is straightforward enough using this:
https://gist.github.com/4353703

- (void)presentCameraOrPhotoLibraryActionSheet
  {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"CameraOrPhotoLibrary"
                                                             delegate:self
                                                    cancelButtonTitle:@"Cancel"
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:@"Take photo or video...", @"Choose from library...", nil];

    [actionSheet showInView:[self tableView]];
}

- (void)actionSheetActionCameraOrPhotoLibrary:(UIActionSheet *)actionSheet clickedButtonWithTitle:(NSString *)buttonTitle
{
    UIImagePickerController *imagePicker;

    if ([buttonTitle isEqualToString:@"Take photo or video..."]) {
        imagePicker = [TCMediaPicker imagePickerForCamera];
    } else if ([buttonTitle isEqualToString:@"Choose from library..."]){
        imagePicker = [TCMediaPicker imagePickerForPhotoGallery];
    }

    [imagePicker setDelegate:self];
    [self presentViewController:imagePicker animated:YES completion:nil];
}

@enriquez
Copy link
Author

I see. I'm trying to solve the problem of the long if..else if... block in a single method. Also, I want to make it easier to pass an object to a button's action without having to store it in a local property.

Your solution solves the multiple action sheet per controller with a naming convention. My app needs a little bit more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment