Skip to content

Instantly share code, notes, and snippets.

@jdriscoll
Created July 26, 2013 18:09
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 jdriscoll/6090959 to your computer and use it in GitHub Desktop.
Save jdriscoll/6090959 to your computer and use it in GitHub Desktop.
Super simple block-based delegates for UIActionSheet and UIAlertView
//
// 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
//
// 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
//
// 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
//
// 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