同一个类有多个alertView, 不同的alertView 点击确定按钮 执行的方法不同
alertOne 点击 确定按钮 执行 methodOne, alertTwo 点击确定按钮 执行 methodTwo
初始化并显示 alertOne
- (IBAction)showAlertOne:(id)sender { UIAlertView *alertOne = [[UIAlertView alloc]initWithTitle:@"AlertOne" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alertOne setTag:1]; [alertOne show]; }
初始化并显示alertTwo
- (IBAction)showAlertTwo:(id)sender { UIAlertView *alertTwo = [[UIAlertView alloc]initWithTitle:@"AlertTwo" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; [alertTwo setTag:2]; [alertTwo show]; }
实现代理
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { return; } if (alertView.tag == 1) { if (buttonIndex == 1) { [self methodOne]; } } else if (alertView.tag == 2) { if (buttonIndex == 1) { [self methodTwo]; } } }
import runtime.h,定义属性关联key
#import "ViewController.h" #import <objc/runtime.h> static void *alertViewKey = "alertViewKey";
初始化并显示 alertOne,同时定义block,将block 与 alertOne 关联
- (IBAction)showAlertOne:(id)sender { UIAlertView *alertOne = [[UIAlertView alloc]initWithTitle:@"AlertOne" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; void (^block)(NSInteger) = ^(NSInteger buttonIndex) { if (buttonIndex == 1) { [self methodOne]; } }; objc_setAssociatedObject(alertOne, alertViewKey, block, OBJC_ASSOCIATION_COPY);//将block 与 alertOne 关联 [alertOne show];
初始化并显示 alertTwo,同时定义block,将block 与 alertTwo 关联
- (IBAction)showAlertTwo:(id)sender { UIAlertView *alertTwo = [[UIAlertView alloc]initWithTitle:@"AlertTwo" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil]; void (^block)(NSInteger) = ^(NSInteger buttonIndex) { if (buttonIndex == 1) { [self methodTwo]; } }; objc_setAssociatedObject(alertTwo, alertViewKey, block, OBJC_ASSOCIATION_COPY); [alertTwo show]; }
实现代理,在代理方法中,直接取出当前alert 所关联上的 block,再将buttonIndex 传入,让block 自己调用对应方法
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex == 0) { return; } void(^block)(NSInteger) = objc_getAssociatedObject(alertView, alertViewKey); block(buttonIndex); }
通过使用Associated object 方法有多个好处 :
简化代码
让代码层次清晰
充分利用对象与属性的拥有关系