Skip to content

Instantly share code, notes, and snippets.

@elpsk
Last active December 26, 2015 03:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elpsk/3d2f5fe1612ff107ad9e to your computer and use it in GitHub Desktop.
Save elpsk/3d2f5fe1612ff107ad9e to your computer and use it in GitHub Desktop.
//
// 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