Skip to content

Instantly share code, notes, and snippets.

@elpsk
Last active Dec 26, 2015
Embed
What would you like to do?
//
// Created by Alberto Pasca on 21/10/13.
// Copyright (c) 2013 albertopasca.it. All rights reserved.
//
@interface UIAlertView (BlockBased)
typedef void (^SelectBlock)(int buttonIndex);
typedef void (^CancelBlock)();
+ (UIAlertView*) showAlertViewWithTitle:(NSString*) title
message:(NSString*) message
cancelButtonTitle:(NSString*) cancelButtonTitle
otherButtonTitles:(NSArray*) otherButtons
onSelect:(SelectBlock) selected
onCancel:(CancelBlock) cancelled;
@end
//
// Created by Alberto Pasca on 21/10/13.
// Copyright (c) 2013 albertopasca.it. All rights reserved.
//
@implementation UIAlertView (BlockBased)
static SelectBlock _selectBlock;
static CancelBlock _cancelBlock;
+ (UIAlertView*) showAlertViewWithTitle:(NSString*) title
message:(NSString*) message
cancelButtonTitle:(NSString*) cancelButtonTitle
otherButtonTitles:(NSArray*) otherButtons
onSelect:(SelectBlock) selected
onCancel:(CancelBlock) cancelled
{
_cancelBlock = [cancelled copy];
_selectBlock = [selected copy];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:title
message:message
delegate:[self self]
cancelButtonTitle:cancelButtonTitle
otherButtonTitles:nil];
for ( NSString *buttonTitle in otherButtons )
[alert addButtonWithTitle:buttonTitle];
[alert show];
return alert;
}
+ (void)alertView:(UIAlertView*) alertView didDismissWithButtonIndex:(NSInteger) buttonIndex
{
if ( buttonIndex == [alertView cancelButtonIndex] ) _cancelBlock();
else _selectBlock (buttonIndex - 1);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment