Created
August 25, 2013 22:10
-
-
Save leapingbytes/6336615 to your computer and use it in GitHub Desktop.
Subclass for UIAlertView which allows to use blocks instead of delegate
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
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]; |
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
#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 |
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
#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