Skip to content

Instantly share code, notes, and snippets.

@jdewind
Created April 7, 2010 16:50
Show Gist options
  • Save jdewind/359120 to your computer and use it in GitHub Desktop.
Save jdewind/359120 to your computer and use it in GitHub Desktop.
+ (void)show:(NSString *)message confirmed:(ConfirmationBlock)confirmBlock canceled:(ConfirmationBlock)canceledBlock {
if(blocks == nil) {
blocks = [[NSMutableDictionary dictionary] retain];
delegate = [[BlockConfirmationAlert alloc] init];
}
UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Confirm" message:message delegate:delegate cancelButtonTitle:@"Cancel" otherButtonTitles:@"Confirm", nil] autorelease];
NSDictionary *blockDict = [NSDictionary dictionaryWithObjectsAndKeys:[[confirmBlock copy] autorelease], @"confirm", [[canceledBlock copy] autorelease], @"cancel", nil];
[blocks setObject:blockDict forKey:[NSNumber numberWithUnsignedInt:[alertView hash]]];
[alertView show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
NSNumber *key = [NSNumber numberWithUnsignedInt:[alertView hash]];
NSDictionary *blockDict = [blocks objectForKey:key];
if (alertView.cancelButtonIndex == buttonIndex) {
((ConfirmationBlock)[blockDict objectForKey:@"cancel"])(alertView);
} else {
((ConfirmationBlock)[blockDict objectForKey:@"confirm"])(alertView);
}
[blocks removeObjectForKey:key];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment