UIAlertView关联属性
#import "FirstViewController.h"
//#import "UIAlertView+TmfUIAlertView.h"
#import <objc/runtime.h>
static const void *alertBlock = "alertBlock";
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"s" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"?", nil];
void(^block)(NSInteger) = ^(NSInteger btnIndex) {
NSLog(@"btnIndex:%ld",(long)btnIndex);
};
objc_setAssociatedObject(self, alertBlock, block, OBJC_ASSOCIATION_COPY);
// alert.block = ^(UIAlertView *alertView) {
//
// NSLog(@"ssss:%@",alertView);
//
// };
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0);
{
void(^block)(NSInteger) = objc_getAssociatedObject(self, alertBlock);
block(buttonIndex);
}
@end
UIAlertView 分类 增加block属性
#import <UIKit/UIKit.h>
typedef void(^alertViewBlock)(UIAlertView *);
@interface UIAlertView (TmfUIAlertView)<UIAlertViewDelegate>
@property(nonatomic, copy) alertViewBlock block;
@end
#import "UIAlertView+TmfUIAlertView.h"
#import <objc/runtime.h>
static const void *tmfAlertViewKey = "tmfAlertViewKey";
@implementation UIAlertView (TmfUIAlertView)
- (void)setBlock:(alertViewBlock) block{
objc_setAssociatedObject(self, tmfAlertViewKey, block, OBJC_ASSOCIATION_COPY);
self.delegate = self;
}
- (alertViewBlock)block{
return objc_getAssociatedObject(self, tmfAlertViewKey);
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0);
{
self.block(alertView);
}