-
-
Save enriquez/4355068 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} | |
} |
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];
}
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
It would be nice if the
UIActionSheet
constructorinitWithTitle: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.