[ios] Callbacks for alerts.

This commit is contained in:
Timur Bernikowich 2014-11-21 19:05:39 +03:00 committed by Alex Zolotarev
parent 49814cc571
commit c96790752a
2 changed files with 206 additions and 0 deletions

View file

@ -72,3 +72,21 @@
@interface SolidTouchImageView : UIImageView
@end
typedef void (^UIAlertViewBlock) (UIAlertView * alertView);
typedef void (^UIAlertViewCompletionBlock) (UIAlertView * alertView, NSInteger buttonIndex);
@interface UIAlertView (Blocks)
@property (copy, nonatomic) UIAlertViewCompletionBlock tapBlock;
@property (copy, nonatomic) UIAlertViewCompletionBlock willDismissBlock;
@property (copy, nonatomic) UIAlertViewCompletionBlock didDismissBlock;
@property (copy, nonatomic) UIAlertViewBlock willPresentBlock;
@property (copy, nonatomic) UIAlertViewBlock didPresentBlock;
@property (copy, nonatomic) UIAlertViewBlock cancelBlock;
@property (copy, nonatomic) BOOL(^shouldEnableFirstOtherButtonBlock)(UIAlertView * alertView);
@end

View file

@ -256,3 +256,191 @@
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {}
@end
#import <objc/runtime.h>
static const void * UIAlertViewOriginalDelegateKey = & UIAlertViewOriginalDelegateKey;
static const void * UIAlertViewTapBlockKey = & UIAlertViewTapBlockKey;
static const void * UIAlertViewWillPresentBlockKey = & UIAlertViewWillPresentBlockKey;
static const void * UIAlertViewDidPresentBlockKey = & UIAlertViewDidPresentBlockKey;
static const void * UIAlertViewWillDismissBlockKey = & UIAlertViewWillDismissBlockKey;
static const void * UIAlertViewDidDismissBlockKey = & UIAlertViewDidDismissBlockKey;
static const void * UIAlertViewCancelBlockKey = & UIAlertViewCancelBlockKey;
static const void * UIAlertViewShouldEnableFirstOtherButtonBlockKey = & UIAlertViewShouldEnableFirstOtherButtonBlockKey;
@implementation UIAlertView (Blocks)
- (void)_checkAlertViewDelegate
{
if (self.delegate != (id<UIAlertViewDelegate>)self)
{
objc_setAssociatedObject(self, UIAlertViewOriginalDelegateKey, self.delegate, OBJC_ASSOCIATION_ASSIGN);
self.delegate = (id <UIAlertViewDelegate>)self;
}
}
- (UIAlertViewCompletionBlock)tapBlock
{
return objc_getAssociatedObject(self, UIAlertViewTapBlockKey);
}
- (void)setTapBlock:(UIAlertViewCompletionBlock)tapBlock
{
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewTapBlockKey, tapBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewCompletionBlock)willDismissBlock
{
return objc_getAssociatedObject(self, UIAlertViewWillDismissBlockKey);
}
- (void)setWillDismissBlock:(UIAlertViewCompletionBlock)willDismissBlock
{
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewWillDismissBlockKey, willDismissBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewCompletionBlock)didDismissBlock
{
return objc_getAssociatedObject(self, UIAlertViewDidDismissBlockKey);
}
- (void)setDidDismissBlock:(UIAlertViewCompletionBlock)didDismissBlock
{
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewDidDismissBlockKey, didDismissBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewBlock)willPresentBlock
{
return objc_getAssociatedObject(self, UIAlertViewWillPresentBlockKey);
}
- (void)setWillPresentBlock:(UIAlertViewBlock)willPresentBlock
{
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewWillPresentBlockKey, willPresentBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewBlock)didPresentBlock
{
return objc_getAssociatedObject(self, UIAlertViewDidPresentBlockKey);
}
- (void)setDidPresentBlock:(UIAlertViewBlock)didPresentBlock
{
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewDidPresentBlockKey, didPresentBlock, OBJC_ASSOCIATION_COPY);
}
- (UIAlertViewBlock)cancelBlock
{
return objc_getAssociatedObject(self, UIAlertViewCancelBlockKey);
}
- (void)setCancelBlock:(UIAlertViewBlock)cancelBlock
{
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewCancelBlockKey, cancelBlock, OBJC_ASSOCIATION_COPY);
}
- (void)setShouldEnableFirstOtherButtonBlock:(BOOL(^)(UIAlertView * alertView))shouldEnableFirstOtherButtonBlock
{
[self _checkAlertViewDelegate];
objc_setAssociatedObject(self, UIAlertViewShouldEnableFirstOtherButtonBlockKey, shouldEnableFirstOtherButtonBlock, OBJC_ASSOCIATION_COPY);
}
- (BOOL(^)(UIAlertView * alertView))shouldEnableFirstOtherButtonBlock
{
return objc_getAssociatedObject(self, UIAlertViewShouldEnableFirstOtherButtonBlockKey);
}
#pragma mark - UIAlertViewDelegate
- (void)willPresentAlertView:(UIAlertView *)alertView
{
UIAlertViewBlock block = alertView.willPresentBlock;
if (block)
block(alertView);
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(willPresentAlertView:)])
[originalDelegate willPresentAlertView:alertView];
}
- (void)didPresentAlertView:(UIAlertView *)alertView
{
UIAlertViewBlock block = alertView.didPresentBlock;
if (block)
block(alertView);
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(didPresentAlertView:)])
[originalDelegate didPresentAlertView:alertView];
}
- (void)alertViewCancel:(UIAlertView *)alertView {
UIAlertViewBlock block = alertView.cancelBlock;
if (block)
block(alertView);
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertViewCancel:)])
[originalDelegate alertViewCancel:alertView];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
UIAlertViewCompletionBlock completion = alertView.tapBlock;
if (completion)
completion(alertView, buttonIndex);
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertView:clickedButtonAtIndex:)])
[originalDelegate alertView:alertView clickedButtonAtIndex:buttonIndex];
}
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex {
UIAlertViewCompletionBlock completion = alertView.willDismissBlock;
if (completion)
completion(alertView, buttonIndex);
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertView:willDismissWithButtonIndex:)])
[originalDelegate alertView:alertView willDismissWithButtonIndex:buttonIndex];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
UIAlertViewCompletionBlock completion = alertView.didDismissBlock;
if (completion)
completion(alertView, buttonIndex);
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertView:didDismissWithButtonIndex:)])
[originalDelegate alertView:alertView didDismissWithButtonIndex:buttonIndex];
}
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
BOOL(^shouldEnableFirstOtherButtonBlock)(UIAlertView * alertView) = alertView.shouldEnableFirstOtherButtonBlock;
if (shouldEnableFirstOtherButtonBlock)
return shouldEnableFirstOtherButtonBlock(alertView);
id originalDelegate = objc_getAssociatedObject(self, UIAlertViewOriginalDelegateKey);
if (originalDelegate && [originalDelegate respondsToSelector:@selector(alertViewShouldEnableFirstOtherButton:)])
return [originalDelegate alertViewShouldEnableFirstOtherButton:alertView];
return YES;
}
@end