利用runtime实现UIAlertView的block回调
来源:互联网 发布:淘宝中文国际版app 编辑:程序博客网 时间:2024/06/03 02:30
平时我们用UIAlertView需要使用其代理方法来确定我们的点击事件,使用起来不够方便,新的sdk中UIAlertViewController是使用block来访问其点击事件的,那我们就将UIAlertView也封装成可以利用block来访问点击事件的类别
首先我们需要一个block属性值
@interface UIAlertView () <UIAlertViewDelegate>
@property (copy,nonatomic) void (^block)(UIAlertView *UIAlertView,NSInteger buttonIndex);
@end
UIAlertView添加按钮是一个个添加,我们可以利用数组来添加
- (instancetype)initWithTitle:(NSString *)title message:(NSString *)message
cancelButtonTitle:(NSString *)cancelButtonTitle
otherButtonTitles:(NSArray *)otherButtonTitles
{
self = [selfinitWithTitle:title message:message
delegate:nil
cancelButtonTitle:cancelButtonTitleotherButtonTitles:nil];
if (self) {
for (NSString *otherButtonTitlein otherButtonTitles) {
[selfaddButtonWithTitle:otherButtonTitle];
}
}
return self;
}
将alertView与block关联起来(通过runtime)注意:要导入头文件
#import <objc/runtime.h>
- (void)setBlock:(void (^)(UIAlertView *,NSInteger))block
{
objc_setAssociatedObject(self,@selector(block), block, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
- (void (^)(UIAlertView *, NSInteger))block
{
return objc_getAssociatedObject(self,@selector(block));
}
当点击alertview 的按钮时
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (self.block) {
self.block(alertView, buttonIndex);
}
}
下面的方法就是block回调
- (void)showUsingBlock:(void (^)(UIAlertView *,NSInteger))block
{
self.delegate =self;
self.block = block;
[selfshow];
}
通过调用此方法,得到的block回调值来判断当前点击的按钮
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle:@""message:@"" delegate:nilcancelButtonTitle:@"cancel" otherButtonTitles:@"ok",nil];
[alert showUsingBlock:^(UIAlertView *alertView,NSInteger buttonIndex) {
}];
- 利用runtime实现UIAlertView的block回调
- UIAlertView利用runtime传值
- UIAlertView使用Block回调
- uialertview block 回调
- iOS 中 Block实现UIAlertView
- UIAlertView + Block
- Objective-C Runtime之着魔的UIAlertView
- 利用runtime实现更加灵活的KVC
- 利用runtime更改系统方法的实现
- iOS 利用runtime实现的Method Swizzling
- 利用runtime更改系统方法的实现
- iOS开发之用block来实现UIAlertView点击事件的代理方法
- UIAlertView的实现
- 利用runtime实现KVO
- 利用Associative机制为UIAlertView添加一个Block属性,将AlertView与button的响应事件关联
- block为什么用copy?利用runtime运行时的objc_方法为分类扩充成员变量
- 二十三 ,iOS UIAlertView使用block来实现代理方法;
- UIAlertView 加Block 扩展
- http://www.52im.net/thread-258-1-1.html
- redis基本操作
- 《C++高效编程:内存与性能优化》第十二章:优化IO
- 使用java将搜狗词库.scel文件转化为.txt文件
- SSM_login
- 利用runtime实现UIAlertView的block回调
- WPF-Dispatcher
- USACO2012 March Gold Large Banner
- iOS- Xcode8更新后输出log日志关闭
- mxnet深度学习实战:跑自己的数据实验和一些问题总结
- java源码分析之集合框架Map 07
- IE11 F12不能用的问题
- Hadoop中命令行解释
- 谈谈MySQL的存储引擎