-
-
Save elpsk/3d2f5fe1612ff107ad9e 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
// | |
// 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 |
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
// | |
// 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