.h文件
#import <UIKit/UIKit.h>typedef void (^successBlock)(NSInteger buttonIndex);@interface UIAlertView (Block)<UIAlertViewDelegate>- (void)showWithBlock:(successBlock)block;@end
#import "UIAlertView+Block.h"
#import <objc/runtime.h>static const char alertKey;@implementation UIAlertView (Block)- (void)showWithBlock:(successBlock)block{ if (block) { objc_setAssociatedObject(self, &alertKey, block, OBJC_ASSOCIATION_RETAIN_NONATOMIC); self.delegate = self; } [self show];}- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ successBlock block = objc_getAssociatedObject(self, &alertKey); block(buttonIndex);}@end
tips: 使用objc_setAssociatedObject,需要引入头文件 #import <objc/runtime.h>
联系客服