iOS 给UIAlertView添加block回调

在iOS开发中,经常用到警告视图与用户交互,虽然在iOS8.0以后推荐使用UIAlertController了,但是对于兼容iOS7.x的项目,有时还是要用UIAlterView。系统是使用了委托进行了回调,把创建视图与处理点击事件的代码分开。当处理事件的逻辑较复杂时,这非常不错。但是当处理逻辑较复杂时,我们一般会抽出某个方法去执行。平时是这样使用UIAlterView的

- (void)someEvent {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"message" delegate:self cancelButtonTitle:@"cancel" otherButtonTitles:@"done", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        // done
    } else {
        //cancel
    }
}
//当然要遵守UIAlertViewDelegate协议

如果想在一个类中处理多个警告视图,那么代码就会变得较为复杂。我们必须在delegate中检查传入的alertView参数,并据此选用相应的处理逻辑。如果能在创建视图的时候,用block进行回调就简单多了。
接下来,我们要做的就是给UIAlertView添加block回调。

1.使用关联对象(AssociatedObject)实现

在Objective-C中,可以使用以下几个方法进行关联对象操作。

  • void objc_setAssociatedObject(id object, void*key, id value, objc_AssociationPolicy policy)
    此方法以给定的键和策略为某对象设置关联对象值
  • id objc_getAssociatedObject(id object, const void *key)
    此方法根据给定的键从某对象中获取相应的关联对象值
  • void objc_removeAssociatedObjects(id object)
    此方法移除指定对象的全部关联对象

实现一个UIAlertView的category,UIAlertView+Extension,定义一个block,并实现show方法。代码如下:

//  UIAlertView+Extension.h
#import <UIKit/UIKit.h>

typedef void(^CopletionBlock)(NSInteger buttonIndex);

@interface UIAlertView (Extension)

- (void)showWithBlock:(CopletionBlock)block;

@end
//  UIAlertView+Extension.m
#import "UIAlertView+Extension.h"
#import <objc/runtime.h>

static void *MyAlterViewKey = "MyAlterViewKey";

@implementation UIAlertView (Extension) 

- (void)showWithBlock:(CopletionBlock)copletionBlock {
    if (copletionBlock) {
        objc_removeAssociatedObjects(self);
        objc_setAssociatedObject(self, MyAlterViewKey, copletionBlock, OBJC_ASSOCIATION_COPY);
        self.delegate = self;
    }
    [self show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    CopletionBlock copletionBlock = objc_getAssociatedObject(self, MyAlterViewKey);
    if (copletionBlock) {
        copletionBlock(buttonIndex);
    }
}

@end

在show的时候将对象与block进行关联,在delegate中获取该block。
使用时,导入UIAlertView+Extension.h文件

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:@"This is message!" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"done", nil];
[alert showWithBlock:^(NSInteger index) {
    if (index == 0) {
        // cancel
    } else {
        // done
    }
}];

2.使用继承实现

继承自UIAlertView实现自定义的子类,在子类中设置block为子类的属性。贴下代码:

//  WSAlterView.h
#import <UIKit/UIKit.h>

typedef void(^CopletionBlock)(NSInteger buttonIndex);

@interface WSAlterView : UIAlertView

- (void)showWithBlock:(CopletionBlock)copletionBlock;

@end
#import "WSAlterView.h"

@interface WSAlterView ()

@property (nonatomic, copy) CopletionBlock copletionBlock;

@end

@implementation WSAlterView

- (void)showWithBlock:(CopletionBlock)copletionBlock {
    if (copletionBlock) {
        self.copletionBlock = copletionBlock;
        self.delegate = self;
    }
    [self show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (self.copletionBlock) {
        self.copletionBlock(buttonIndex);
    }
}

@end

使用时,先导入WSAlterView.h。使用代码如下:

WSAlterView *alert = [[WSAlterView alloc] initWithTitle:@"title" message:@"This is message!" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"done", nil];
[alert showWithBlock:^(NSInteger index) {
    if (index == 0) {
        // cancel
    } else {
        // done
    }
}];

至此,为UIAlertView添加block回调就用两种方式实现了。
Demo在这里

注意:

需要注意的是,如果在类中强引用了UIAlertView,而警告视图的block回调中又强引用这个类的话,就会形成循环引用。比如在某个控制器中强引用一个UIAlertView,循环引用是这样的
VC—>alertView—>block—>VC
这种情况需要用__weak处理下。

有错误的地方或更改的实现方式,请留言讨论,谢谢!

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 转至元数据结尾创建: 董潇伟,最新修改于: 十二月 23, 2016 转至元数据起始第一章:isa和Class一....
    40c0490e5268阅读 1,827评论 0 9
  • iOS代码块Block 概述 代码块Block是苹果在iOS4开始引入的对C语言的扩展,用来实现匿名函数的特性,B...
    smile刺客阅读 2,377评论 2 26
  • 前言 Blocks是C语言的扩充功能,而Apple 在OS X Snow Leopard 和 iOS 4中引入了这...
    小人不才阅读 3,790评论 0 23
  • 本文是战隼老师在喜马拉雅FM的《战隼:无需意思志力的习惯养成法-100天行动》的笔记。如需要原文,请收听节目。如有...
    西西西西力阅读 395评论 0 1
  • Arthur is very busy recently. As a four years old boy, he...
    老蛮腰阅读 333评论 0 1