Skip to content

Instantly share code, notes, and snippets.

@leapingbytes
Created August 25, 2013 22:10
Show Gist options
  • Save leapingbytes/6336615 to your computer and use it in GitHub Desktop.
Save leapingbytes/6336615 to your computer and use it in GitHub Desktop.
Subclass for UIAlertView which allows to use blocks instead of delegate
LBAlertView *alert = [[LBAlertView alloc] initWithTitle:@"Title" message: @"message" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles: @"Enter", nil];
alert.onClickBlock = ^(UIAlertView* alertView, int buttonIndex) {
if (buttonIndex == 1) {
// DO something
}
else {
// Do something else
}
};
[alert show];
#import <UIKit/UIKit.h>
typedef void (^LB_ALERT_BLOC)(UIAlertView* alertView, int buttonIndex);
@interface LBAlertView: UIAlertView<UIAlertViewDelegate>
@property(strong, nonatomic) LB_ALERT_BLOC onClickBlock_;
- (void) setOnClickBlock: (LB_ALERT_BLOC) aBlock;
@end
#import "UIAlertView+LBToolbox.h"
@implementation LBAlertView
- (LB_ALERT_BLOC) onClickBlock {
return self.onClickBlock_;
}
- (void) setOnClickBlock: (LB_ALERT_BLOC) aBlock {
self.delegate = self;
self.onClickBlock_ = aBlock;
}
- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
self.onClickBlock_(alertView, buttonIndex);
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment