diff --git a/UIAlertView/UIAlertView+Block.h b/UIAlertView/UIAlertView+Block.h index cfa7d36..7ac4f8d 100644 --- a/UIAlertView/UIAlertView+Block.h +++ b/UIAlertView/UIAlertView+Block.h @@ -23,4 +23,12 @@ completionBlock:(void (^)(UIAlertView* alertView, NSInteger selectedButtonIndex)) completionBlock cancelBlock:(void (^)()) cancelBlock; +- (id)initWithTitle:(NSString *)title + message:(NSString *)message + cancelButtonTitle:(NSString *)cancelButtonTitle + otherButtonTitles:(NSArray*) titles + completionBlock:(void (^)(UIAlertView* alertView, NSInteger selectedButtonIndex)) completionBlock + cancelBlock:(void (^)()) cancelBlock + didDismissBlock:(void (^)(UIAlertView* alertView, NSInteger selectedButtonIndex)) didDismissBlock; + @end diff --git a/UIAlertView/UIAlertView+Block.m b/UIAlertView/UIAlertView+Block.m index 064137b..58e18fc 100644 --- a/UIAlertView/UIAlertView+Block.m +++ b/UIAlertView/UIAlertView+Block.m @@ -12,6 +12,7 @@ @interface UIAlertView() @property (nonatomic, copy) void (^completionBlock)(UIAlertView* alertView, NSInteger selectedButtonIndex); @property (nonatomic, copy) void (^cancelBlock)(); +@property (nonatomic, copy) void (^didDismissBlock)(UIAlertView* alertView, NSInteger selectedButtonIndex); @end @implementation UIAlertView (Block) @@ -44,6 +45,19 @@ - (id)initWithTitle:(NSString *)title return self; } +- (id)initWithTitle:(NSString *)title + message:(NSString *)message + cancelButtonTitle:(NSString *)cancelButtonTitle + otherButtonTitles:(NSArray*) titles + completionBlock:(void (^)(UIAlertView* alertView, NSInteger selectedButtonIndex)) completionBlock + cancelBlock:(void (^)()) cancelBlock + didDismissBlock:(void (^)(UIAlertView* alertView, NSInteger selectedButtonIndex)) didDismissBlock { + if (self = [self initWithTitle:title message:message cancelButtonTitle:cancelButtonTitle otherButtonTitles:titles completionBlock:completionBlock cancelBlock:cancelBlock]) { + self.didDismissBlock = didDismissBlock; + } + return self; +} + #pragma mark UIAlertViewDelegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { @@ -61,6 +75,15 @@ - (void) alertViewCancel:(UIAlertView *)alertView { } self.completionBlock = nil; } + +- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex +{ + if (self.didDismissBlock) { + self.didDismissBlock(self, buttonIndex); + self.didDismissBlock = nil; + } +} + #pragma mark - Private - (void) setCompletionBlock:(void (^)(UIAlertView*, NSInteger))completionBlock { @@ -79,5 +102,13 @@ - (void) setCancelBlock:(void (^)())cancelBlock { return objc_getAssociatedObject(self, @"cancelBlock"); } +- (void) setDidDismissBlock:(void (^)(UIAlertView*, NSInteger))didDismissBlock { + objc_setAssociatedObject(self, @"didDismissBlock", [didDismissBlock copy], OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (void(^)(UIAlertView*, NSInteger)) didDismissBlock { + return objc_getAssociatedObject(self, @"didDismissBlock"); +} + @end