- Add Alert block
This commit is contained in:
1 parent
dfd5420089
commit
ab1b14b4e2
43 files changed
+1435
-353
No files matched your search
+21
@@ -0,0 +1,21 @@
|
||||
//
|
||||
// AlertViewActionDispatcher.h
|
||||
// AlertViewExamples
|
||||
//
|
||||
// Created by Mango on 2015. 3. 18..
|
||||
// Copyright (c) 2015년 Mangofever. All rights reserved.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
#define ActionByCancel -1
|
||||
|
||||
typedef void (^UIAlertActionBlock) (UIAlertView *alertView, NSInteger buttonIndex);
|
||||
|
||||
@interface AlertViewActionDispatcher : NSObject
|
||||
|
||||
- (void)addActionBlock:(UIAlertActionBlock)actionBlock withButtonIndex:(NSInteger)buttonIndex;
|
||||
- (void)addCancelActionBlock:(UIAlertActionBlock)actionBlock;
|
||||
|
||||
@end
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
//
|
||||
// AlertViewActionDispatcher.m
|
||||
// AlertViewExamples
|
||||
//
|
||||
// Created by Mango on 2015. 3. 18..
|
||||
// Copyright (c) 2015년 Mangofever. All rights reserved.
|
||||
//
|
||||
|
||||
#import "AlertViewActionDispatcher.h"
|
||||
|
||||
@interface AlertViewActionDispatcher ()
|
||||
|
||||
@property (nonatomic, strong) NSMutableDictionary *actionBlockDictionary;
|
||||
|
||||
@end
|
||||
|
||||
@implementation AlertViewActionDispatcher
|
||||
|
||||
- (id)init {
|
||||
self = [super init];
|
||||
if (self) {
|
||||
self.actionBlockDictionary = [NSMutableDictionary dictionary];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)addActionBlock:(UIAlertActionBlock)actionBlock withButtonIndex:(NSInteger)buttonIndex {
|
||||
if (actionBlock) {
|
||||
[self.actionBlockDictionary setObject:actionBlock forKey:@(buttonIndex)];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)addCancelActionBlock:(UIAlertActionBlock)actionBlock {
|
||||
if (actionBlock) {
|
||||
[self.actionBlockDictionary setObject:actionBlock forKey:@(ActionByCancel)];
|
||||
}
|
||||
}
|
||||
|
||||
- (BOOL)excuteActionBlockForAlertView:(UIAlertView *)alertView buttonIndex:(NSInteger)buttonIndex {
|
||||
UIAlertActionBlock actionBlock = [self.actionBlockDictionary objectForKey:@(buttonIndex)];
|
||||
if (actionBlock) {
|
||||
actionBlock(alertView, buttonIndex);
|
||||
return YES;
|
||||
}
|
||||
return NO;
|
||||
}
|
||||
|
||||
#pragma mark - UIAlertViewDelegate
|
||||
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
|
||||
[self excuteActionBlockForAlertView:alertView buttonIndex:buttonIndex];
|
||||
}
|
||||
|
||||
- (void)alertViewCancel:(UIAlertView *)alertView {
|
||||
if ([self excuteActionBlockForAlertView:alertView buttonIndex:ActionByCancel] == NO) {
|
||||
[self excuteActionBlockForAlertView:alertView buttonIndex:alertView.cancelButtonIndex];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
//
|
||||
// UIAlertView+BlockExtension.h
|
||||
// AlertViewExamples
|
||||
//
|
||||
// Created by Mango on 2015. 3. 18..
|
||||
// Copyright (c) 2015년 Mangofever. All rights reserved.
|
||||
//
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import "AlertViewActionDispatcher.h"
|
||||
|
||||
@interface UIAlertView (BlockExtension)
|
||||
|
||||
@property (strong, nonatomic) AlertViewActionDispatcher *actionDispatcher;
|
||||
|
||||
+ (UIAlertView *)alertViewWithTitle:(NSString *)title message:(NSString *)message;
|
||||
|
||||
- (void)addButtonWithTitle:(NSString *)title actionBlock:(UIAlertActionBlock)actionBlock;
|
||||
- (void)addCancelButtonWithTitle:(NSString *)title actionBlock:(UIAlertActionBlock)actionBlock;
|
||||
- (void)addCancelActionBlock:(UIAlertActionBlock)actionBlock;
|
||||
|
||||
@end
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
//
|
||||
// UIAlertView+BlockExtension.m
|
||||
// AlertViewExamples
|
||||
//
|
||||
// Created by Mango on 2015. 3. 18..
|
||||
// Copyright (c) 2015년 Mangofever. All rights reserved.
|
||||
//
|
||||
|
||||
#import "UIAlertView+BlockExtension.h"
|
||||
#import <objc/runtime.h>
|
||||
|
||||
@implementation UIAlertView (BlockExtension)
|
||||
|
||||
@dynamic actionDispatcher;
|
||||
|
||||
+ (UIAlertView *)alertViewWithTitle:(NSString *)title message:(NSString *)message {
|
||||
|
||||
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
|
||||
alertView.actionDispatcher = [[AlertViewActionDispatcher alloc] init];
|
||||
|
||||
return alertView;
|
||||
}
|
||||
|
||||
- (void)addButtonWithTitle:(NSString *)title actionBlock:(UIAlertActionBlock)actionBlock {
|
||||
NSInteger buttonIndex = [self addButtonWithTitle:title];
|
||||
[self.actionDispatcher addActionBlock:actionBlock withButtonIndex:buttonIndex];
|
||||
}
|
||||
|
||||
- (void)addCancelButtonWithTitle:(NSString *)title actionBlock:(UIAlertActionBlock)actionBlock {
|
||||
NSInteger buttonIndex = [self addButtonWithTitle:title];
|
||||
self.cancelButtonIndex = buttonIndex;
|
||||
[self.actionDispatcher addActionBlock:actionBlock withButtonIndex:buttonIndex];
|
||||
}
|
||||
|
||||
- (void)addCancelActionBlock:(UIAlertActionBlock)actionBlock {
|
||||
[self.actionDispatcher addCancelActionBlock:actionBlock];
|
||||
}
|
||||
|
||||
|
||||
- (void)setActionDispatcher:(AlertViewActionDispatcher *)actionDispatcher {
|
||||
self.delegate = actionDispatcher;
|
||||
objc_setAssociatedObject(self, @selector(actionDispatcher), actionDispatcher, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
- (AlertViewActionDispatcher *)actionDispatcher {
|
||||
return objc_getAssociatedObject(self, @selector(actionDispatcher));
|
||||
}
|
||||
|
||||
|
||||
@end
|
||||
Reference in new issue
Block a user