Created
July 26, 2013 18:09
-
-
Save jdriscoll/6090959 to your computer and use it in GitHub Desktop.
Super simple block-based delegates for UIActionSheet and UIAlertView
This file contains hidden or 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
// | |
// JCDActionSheetDelegate.h | |
// Created by Justin Driscoll on 4/3/13. | |
// | |
#import <UIKit/UIKit.h> | |
typedef void (^JCDActionSheetDelegateCallback)(UIActionSheet *actionSheet, NSInteger buttonIndex); | |
@interface JCDActionSheetDelegate : NSObject <UIActionSheetDelegate> | |
- (id)initWithCallback:(JCDActionSheetDelegateCallback)callback; | |
@end |
This file contains hidden or 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
// | |
// JCDActionSheetDelegate.m | |
// Created by Justin Driscoll on 4/3/13. | |
// | |
#import "JCDActionSheetDelegate.h" | |
@interface JCDActionSheetDelegate () { | |
JCDActionSheetDelegate *_this; | |
JCDActionSheetDelegateCallback _callback; | |
} | |
@end | |
@implementation JCDActionSheetDelegate | |
- (id)initWithCallback:(JCDActionSheetDelegateCallback)callback | |
{ | |
self = [super init]; | |
if (self) { | |
// We need to retain ourselves | |
_this = self; | |
_callback = [callback copy]; | |
} | |
return self; | |
} | |
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex | |
{ | |
_callback(actionSheet, buttonIndex); | |
// We can release ourselves now | |
_this = nil; | |
} | |
@end |
This file contains hidden or 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
// | |
// JCDAlertViewDelegate.h | |
// Created by Justin Driscoll on 7/26/13. | |
// | |
#import <Foundation/Foundation.h> | |
typedef void (^JCDAlertViewDelegateCallback)(UIAlertView *alertView, NSInteger buttonIndex); | |
@interface JCDAlertViewDelegate : NSObject <UIAlertViewDelegate> | |
- (id)initWithCallback:(JCDAlertViewDelegateCallback)callback; | |
@end |
This file contains hidden or 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
// | |
// JCDAlertViewDelegate.m | |
// Created by Justin Driscoll on 7/26/13. | |
// | |
#import "JCDAlertViewDelegate.h" | |
@interface JCDAlertViewDelegate () { | |
JCDAlertViewDelegate *_this; | |
JCDAlertViewDelegateCallback _callback; | |
} | |
@end | |
@implementation JCDAlertViewDelegate | |
- (id)initWithCallback:(JCDAlertViewDelegateCallback)callback | |
{ | |
self = [super init]; | |
if (self) { | |
// We need to retain ourselves | |
_this = self; | |
_callback = [callback copy]; | |
} | |
return self; | |
} | |
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex | |
{ | |
_callback(alertView, buttonIndex); | |
// We can release ourselves now | |
_this = nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment