Created
March 22, 2012 20:37
-
-
Save pcperini/2163891 to your computer and use it in GitHub Desktop.
Create Block-Based UIAlertViews Without the Hassle
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
// | |
// UIAlertView+Blocks.h | |
// | |
// Created by Patrick Perini on 3/22/12. | |
// Copyright (c) 2012 Inspyre Technologies. MIT License. | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIAlertView (Blocks) <UIAlertViewDelegate> | |
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle cancelButtonBlock:(void (^)())cancelBlock otherButtonTitlesAndBlocks:(id)otherButtonTitlesAndBlocks, ... NS_REQUIRES_NIL_TERMINATION; | |
@end | |
/* | |
Example Use: | |
void (^cancelBlock)() = ^() | |
{ | |
NSLog(@"Canceled"); | |
}; | |
void (^okBlock)() = ^() | |
{ | |
NSLog(@"OK!"); | |
}; | |
UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle: @"ALERT" | |
message: @"Gonna do some stuff, ok?" | |
cancelButtonTitle: @"Cancel" | |
cancelButtonBlock: cancelBlock | |
otherButtonTitlesAndBlocks: @"OK", okBlock, nil]; | |
[myAlertView show]; | |
If the user presses the "Cancel" button, the word "Canceled" will log. | |
Likewise, if the user presses the "OK" button, the word "OK!" will log. | |
To add more buttons, alternate between button titles and their blocks for `otherButtonTitlesAndBlocks`. | |
Think NSDictionary's objectsAndKeys... | |
*/ |
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
// | |
// UIAlertView+Blocks.m | |
// | |
// Created by Patrick Perini on 3/22/12. | |
// Copyright (c) 2012 Inspyre Technologies. MIT License. | |
// | |
#import "UIAlertView+Blocks.h" | |
#import <objc/runtime.h> | |
@implementation UIAlertView (Blocks) | |
static void *blocksKey; | |
- (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle cancelButtonBlock:(void (^)())cancelBlock otherButtonTitlesAndBlocks:(id)otherButtonTitlesAndBlocks, ... | |
{ | |
self = [self initWithTitle: title | |
message: message | |
delegate: self | |
cancelButtonTitle: cancelButtonTitle | |
otherButtonTitles: nil]; | |
if (self) | |
{ | |
va_list args; | |
va_start(args, otherButtonTitlesAndBlocks); | |
NSMutableArray *buttonBlocks = [NSMutableArray array]; | |
[buttonBlocks addObject: cancelBlock]; | |
for (NSString *arg = otherButtonTitlesAndBlocks; arg != nil; arg = va_arg(args, NSString *)) | |
{ | |
[self addButtonWithTitle: arg]; | |
[buttonBlocks addObject: va_arg(args, void (^)())]; | |
} | |
va_end(args); | |
objc_setAssociatedObject(self, | |
blocksKey, | |
buttonBlocks, | |
OBJC_ASSOCIATION_COPY_NONATOMIC); | |
} | |
return self; | |
} | |
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex | |
{ | |
NSMutableArray *buttonBlocks = objc_getAssociatedObject(self, | |
blocksKey); | |
((void (^)())[buttonBlocks objectAtIndex: buttonIndex])(); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment