iOS关联的一些实际用法
2015-04-02 13:26
183 查看
之前写了一些关联的一些基本用法,有些朋友说理论性太强,没有实际的Demo。于是在UIAlertView上写了一个Demo.可以实现UIAlertView的回调操作.具体如下
Category.h
ViewController.m中的用法
附上Demo下载链接:http://download.csdn.net/detail/u014466582/8557275
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; @endCategory.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
相关文章推荐
- ios关联的一些用法
- 关于IOS KVC的具体用法,一些苹果文档没写出来的细节
- 【编程技巧】 iOS 5的StoryBoard(故事板)的一些用法
- iOS OC -date日期的类的一些关键用法
- IOS学习之NSUserDefaults的一些用法
- IOS UI Label的一些用法
- NSSet NSArray的区别 实际用法 IOS
- IOS:pch的一些用法
- iOS中定时器NSTimer的一些用法总结
- iOS开发 UITextView(多行文本框)控件的一些用法(显示文字 退出键盘等)
- 关于iOS NSDate的一些用法
- IOS 导航栏的 一些用法
- iOS NSNotification通知和KVC KVO 的一些用法
- ios打开第三方应用的用法及一些URL
- IOS开发-CGAffineTransform的用法,实现一些简单动画效果
- IOS 自带二维码扫描相关类的一些定义及用法
- IOS开发(31)之关于self.用法的一些总结(转载)
- IOS CGAffineTransform一些用法
- IOS block的一些用法例子
- 【Appium系列之iOS平台】关于滑动的一些用法