UIAlertView使用Block回调

作者: shaneZhang 分类: ios技术 发布时间: 2014-12-22 17:43

//
//  UIAlertView+ShaneZhang.m
//  BlockDemoProject
//
//  Created by shanezhang on 14/12/22.
//  Copyright (c) 2014年 ShaneZhang. All rights reserved.
//

 #import "UIAlertView+ShaneZhang.h"

 #import 

@implementation UIAlertView (ShaneZhang)
static char key;
- (void)showAlertViewWithCompleteBlock:(CompleteBlock)block
{
    if (block)
    {
        objc_removeAssociatedObjects(self);
        objc_setAssociatedObject(self, &key, block, OBJC_ASSOCIATION_COPY);
        self.delegate = self;
    }
    
    [self show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    ///获取关联的对象,通过关键字。
    CompleteBlock block = objc_getAssociatedObject(self, &key);
    if (block) 
    {
        ///block传值
        block(buttonIndex);
    }
}


/**
 OC中的关联就是在已有类的基础上添加对象参数。来扩展原有的类,需要引入#import 头文件。关联是基于一个key来区分不同的关联。
 常用函数: objc_setAssociatedObject     设置关联
 objc_getAssociatedObject     获取关联
 objc_removeAssociatedObjects 移除关联
 */
@end

本页面支持繁体中文友好显示:UIAlertView使用Block回调

如果觉得我的文章对您有用,请随意打赏。如果有其他问题请联系博主QQ(909491009)或者下方留言!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注