- Add Alert block

This commit is contained in:
giuseppenuc committed 2015-08-18 15:00:07 +02:00
1 parent dfd5420089
commit ab1b14b4e2
43 files changed
+1435 -353

No files matched your search

@@ -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
@@ -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
@@ -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
@@ -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
+21
View File
@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2015 mangofever
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+32
View File
@@ -0,0 +1,32 @@
# UIAlertViewBlockExtension
UIAlertView category for block expression
## Installation
### Using cocoapods
<code>
pod 'UIAlertViewBlockExtension'
</code>
## Features
- Block expression can be used for handling UIAlertView's clicks.
- Similar with UIAlertController in iOS 8.0
## Examples
```
UIAlertView *alertView = [UIAlertView alertViewWithTitle:@"title" message:@"message"];
[alertView addButtonWithTitle:@"OK" actionBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
NSLog(@"clicked button index : %lu", buttonIndex);
NSLog(@"cancel button index : %lu", alertView.cancelButtonIndex);
}];
[alertView addCancelButtonWithTitle:@"Cancel" actionBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
NSLog(@"clicked button index : %lu", buttonIndex);
NSLog(@"cancel button index : %lu", alertView.cancelButtonIndex);
}];
[alertView show];
```
## Requirements
- iOS version 4.0 or later