-
-
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 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
I think this is straightforward enough using this:
https://gist.github.com/4353703