之前写了一些关联的一些基本用法,有些朋友说理论性太强,没有实际的Demo。于是在UIAlertView上写了一个Demo.可以实现UIAlertView的回调操作.具体如下
Category.h
//
// UIAlertView+yw.h
// UIAlterViewDemo
//
// Created by yuanwei on 15-4-2.
// Copyright (c) 2015年 YuanWei. All rights reserved.
//
#import <UIKit/UIKit.h>
typedef void(^yw_void_block_int)(int index);
@interface UIAlertView (yw)<UIAlertViewDelegate>
+ (void)dismiss:(BOOL)animated;
+ (UIAlertView *)showAlertWithTitle:(NSString *)title
message:(NSString *)message
buttons:(NSArray *)buttons
handlerBlock:(yw_void_block_int)handlerBlock;
+ (UIAlertView *)initWithTitle:(NSString *)title
message:(NSString *)message
buttons:(NSArray *)buttons;
- (void)setCompletionHandlerBlock:(yw_void_block_int)handlerBlcok;
- (void)dismiss:(BOOL)animated;
@end
Category.m
//
// UIAlertView+yw.m
// UIAlterViewDemo
//
// Created by yuanwei on 15-4-2.
// Copyright (c) 2015年 YuanWei. All rights reserved.
//
#import "UIAlertView+yw.h"
#import <objc/runtime.h>
#define AlertCompletionHandlerBlock @"AlertCompletionHandlerBlock"
static UIAlertView *ywAlertView;
//两种写法都行
static char yw_InjectDict_Key;
//const void *yw_InjectDict_Key;
//也可以直接这样写
//const void *yw_InjectDict_Key = &yw_InjectDict_Key;
@implementation UIAlertView (yw)
+ (void)dismiss:(BOOL)animated
{
if (ywAlertView) {
[ywAlertView dismiss:animated];
}
}
+ (UIAlertView *)showAlertWithTitle:(NSString *)title
message:(NSString *)message
buttons:(NSArray *)buttons
handlerBlock:(yw_void_block_int)handlerBlock
{
UIAlertView *alertView = [self initWithTitle:title
message:message
buttons:buttons];
[alertView setCompletionHandlerBlock:handlerBlock];
[alertView show];
return alertView;
}
+ (UIAlertView *)initWithTitle:(NSString *)title
message:(NSString *)message
buttons:(NSArray *)buttons
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:nil
otherButtonTitles:nil];
for (NSString *btnName in buttons){
[alertView addButtonWithTitle:btnName];
}
//防止和正常的业务逻辑冲突
alertView.cancelButtonIndex = -1;
ywAlertView = alertView;
return alertView;
}
- (yw_void_block_int)getCompletionHandlerBlock
{
yw_void_block_int handlerBlcok = [self getInjectBlockByIdentifier:AlertCompletionHandlerBlock];
return handlerBlcok;
}
- (id)getInjectBlockByIdentifier:(NSString *)identifier
{
id obj = nil;
NSAssert(identifier != nil, @"根据关键字取关联对象的值。关键不能为空");
NSDictionary *injectDict = objc_getAssociatedObject(self,&yw_InjectDict_Key);
if(injectDict){
obj = injectDict[identifier];
}
return obj;
}
- (void)setCompletionHandlerBlock:(yw_void_block_int)handlerBlcok
{
self.delegate = (id<UIAlertViewDelegate>)self;
[self injectBlock:handlerBlcok withIdentifier:AlertCompletionHandlerBlock];
}
- (void)injectBlock:(id)block withIdentifier:(NSString *)identifier
{
if (block) {
id newBlock = [block copy];
NSAssert(identifier != nil, @"创建关联,关键字不能为空");
NSMutableDictionary *injectDict = objc_getAssociatedObject(self,&yw_InjectDict_Key);
if (!injectDict) {
injectDict = [NSMutableDictionary dictionary];
objc_setAssociatedObject(self, &yw_InjectDict_Key, injectDict, OBJC_ASSOCIATION_RETAIN);
}
injectDict[identifier] = newBlock;
}
}
- (void)dismiss:(BOOL)animated
{
[self dismissWithClickedButtonIndex:self.cancelButtonIndex animated:animated];
}
#pragma mark - UIAlertViewDelegateMethod
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
yw_void_block_int handlerBlcok = [self getCompletionHandlerBlock];
if(handlerBlcok){
handlerBlcok((int)buttonIndex);
}
[self removeInjectBlockByIdentifier:AlertCompletionHandlerBlock];
}
- (void)removeInjectBlockByIdentifier:(NSString *)identifier
{
NSAssert(identifier != nil, @"根据关键字,移除对应的关联值,关键字不能为空");
NSMutableDictionary *injectDict = objc_getAssociatedObject(self,&yw_InjectDict_Key);
if(injectDict){
[injectDict removeObjectForKey:identifier];
}
}
@end
ViewController.m中的用法
//
// ViewController.m
// UIAlterViewDemo
//
// Created by yuanwei on 15-4-2.
// Copyright (c) 2015年 YuanWei. All rights reserved.
//
#import "ViewController.h"
#import "UIAlertView+yw.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)handClick:(id)sender
{
[UIAlertView showAlertWithTitle:nil
message:@"请选择出行方式"
buttons:@[@"火车",@"飞机",@"取消"]
handlerBlock:^(int index) {
switch (index) {
case 0:{
NSLog(@"%d %@",index,@"火车");
}break;
case 1:{
NSLog(@"%d %@",index,@"飞机");
}break;
default:
break;
}
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
附上Demo下载链接:http://download.csdn.net/detail/u014466582/8557275