Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions UIAlertView/UIAlertView+Block.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 31 additions & 0 deletions UIAlertView/UIAlertView+Block.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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