用block改写UIButton点击事件和UIAlerView的按钮点击代理方法
2015-09-12 11:00 小松wolf 阅读(1166) 评论(0) 编辑 收藏 举报1.用block改写UIButton点击事件
在这里给给出两种方式.
(1)自定义BlockButton,在初始化的时候给出按钮的样式(自定义)
自定义一个BlockButton继承UIButton,然后在里面用
addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
这个方法触发block.
MyBlockButton.h
1 #import <UIKit/UIKit.h> 2 3 @class MyBlockButton; // 此处一定要声明 4 typedef void(^TouchBlock)(MyBlockButton *button); 5 6 @interface MyBlockButton : UIButton 7 8 @property (copy, nonatomic) TouchBlock block; 9 10 @end
MyBlockButton.m
1 #import "MyBlockButton.h" 2 3 @implementation MyBlockButton 4 5 -(instancetype)initWithFrame:(CGRect)frame 6 { 7 self = [super initWithFrame:frame]; 8 9 if (self) { 10 // 按钮边框美化 11 self.layer.borderWidth = 1; 12 self.layer.borderColor = [UIColor lightGrayColor].CGColor; 13 self.layer.cornerRadius = 3; 14 self.layer.masksToBounds = YES; 15 16 // 为按钮添加阴影 17 self.layer.shadowColor = [UIColor blackColor].CGColor; 18 self.layer.shadowOffset = CGSizeMake(3, 3); 19 self.layer.shadowRadius = 3; 20 21 // 调用此方法以触发block 22 [self addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside]; 23 } 24 return self; 25 } 26 27 - (void)clickAction:(MyBlockButton *)button 28 { 29 _block(button); 30 } 31 32 @end
ViewController.h里面没有添加任何代码
ViewController.m
1 #import "ViewController.h" 2 #import "MyBlockButton.h" 3 4 @interface ViewController () 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 // 创建一个"确定按钮" 14 MyBlockButton *button = [[MyBlockButton alloc]initWithFrame:CGRectMake(35, 100, 260, 30)]; 15 [button setTitle:@"确定" forState:UIControlStateNormal]; 16 [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 17 button.backgroundColor = [UIColor orangeColor]; 18 [button setBlock:^(MyBlockButton *blockButton) { 19 NSLog(@"按钮被点击了"); 20 }]; 21 [self.view addSubview:button]; 22 23 // 创建三个数字按钮 24 for (int i = 1; i <= 3; i ++) { 25 MyBlockButton *btn = [[MyBlockButton alloc]initWithFrame:CGRectMake(70 * i, 200, 60, 30)]; 26 [btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal]; 27 [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 28 btn.backgroundColor = [UIColor redColor]; 29 btn.block = ^(MyBlockButton *blockBtn) { 30 NSLog(@"按钮%d被点击了",i); 31 }; 32 33 [self.view addSubview:btn]; 34 } 35 } 36 37 - (void)didReceiveMemoryWarning { 38 [super didReceiveMemoryWarning]; 39 // Dispose of any resources that can be recreated. 40 } 41 42 @end
其运行效果如下:
(2)接下来给出的方法中,使用自定义方法调用block;继承自UIButton,在初始化使用的时候还需要设置其样式.
MyButtonBlock.h
1 #import <UIKit/UIKit.h> 2 3 typedef void(^TouchBlock)(UIButton *button); 4 5 @interface MyButtonBlock : UIButton 6 7 @property (copy, nonatomic) TouchBlock block; 8 9 // 自定义方法,调用block 10 - (void)clikAction:(TouchBlock)block; 11 12 @end
MyButtonBlock.m
1 #import "MyButtonBlock.h" 2 3 @implementation MyButtonBlock 4 5 - (void)clikAction:(TouchBlock)block 6 { 7 _block = block; 8 // 调用此方法以触发block 9 [self addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside]; 10 } 11 12 - (void)clickButton:(UIButton *)button 13 { 14 _block(button); 15 } 16 17 @end
ViewController.h里面没有添加任何代码
ViewController.m
1 #import "ViewController.h" 2 #import "MyButtonBlock.h" 3 4 @interface ViewController () 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 MyButtonBlock *buttonBlock = [MyButtonBlock buttonWithType:UIButtonTypeCustom]; 14 buttonBlock.frame = CGRectMake(35, 100, 300, 30); 15 [buttonBlock setTitle:@"确定" forState:UIControlStateNormal]; 16 [buttonBlock setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 17 buttonBlock.backgroundColor = [UIColor orangeColor]; 18 19 [buttonBlock clikAction:^(UIButton *button) { 20 NSLog(@"按钮被点击了!"); 21 }]; 22 23 [self.view addSubview:buttonBlock]; 24 } 25 26 - (void)didReceiveMemoryWarning { 27 [super didReceiveMemoryWarning]; 28 // Dispose of any resources that can be recreated. 29 } 30 31 @end
其运行效果如下:
2.用block改写UIButton点击事件和UIAlerView的代理
下面再改写Alert这个控件,思路与改写UIButton的点击事件的第二种方法是一样的,在自定义的Alert里面用block触发点击事件,而在Alert定义的代码里执行事件触发的行为.
MyAlertViewBlock.h
1 #import <UIKit/UIKit.h> 2 3 typedef void(^TouchBlock)(NSInteger buttonIndex); 4 5 @interface MyAlertViewBlock : UIAlertView 6 7 @property (copy, nonatomic) TouchBlock block; 8 9 // 自定义初始化方法 10 - (instancetype)initWithTitle:(NSString *)title 11 message:(NSString *)message 12 cancelButtonTitle:(NSString *)cancelButtonTitle 13 otherButtonTitles:(NSString *)otherButtonTitles 14 andTouchBlock:(TouchBlock)block; 15 16 @end
MyAlertViewBlock.m
1 #import "MyAlertViewBlock.h" 2 3 @implementation MyAlertViewBlock 4 5 - (instancetype)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles andTouchBlock:(TouchBlock)block 6 { 7 // 先初始化父类的方法 8 self = [super initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:otherButtonTitles, nil]; 9 if (self) { 10 self.block = block; 11 } 12 return self; 13 } 14 15 #pragma mark - UIAlertViewDelegate(这只是一个提示,不需要签订代理协议) 16 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 17 { 18 _block(buttonIndex); 19 } 20 21 @end
ViewController.h里面没有添加任何代码
ViewController.m
1 #import "ViewController.h" 2 #import "MyAlertViewBlock.h" 3 4 @interface ViewController () 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 // 创建一个UIButton(用于弹出UIAlertView) 14 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 15 button.frame = CGRectMake(35, 100, 260, 30); 16 button.backgroundColor = [UIColor orangeColor]; 17 [button setTitle:@"确定" forState:UIControlStateNormal]; 18 [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; 19 [button addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside]; 20 [self.view addSubview:button]; 21 } 22 23 // 按钮点击方法 24 - (void)clickAction:(UIButton *)button { 25 MyAlertViewBlock *alertView = [[MyAlertViewBlock alloc]initWithTitle:@"测试"message:@"将UIAlertView的按钮点击事件代理用block实现" cancelButtonTitle:@"取消"otherButtonTitles:@"确定"andTouchBlock:^(NSInteger buttonIndex) { 26 //在这里面执行触发的行为,省掉了代理,这样的好处是在使用多个Alert的时候可以明确定义各自触发的行为,不需要在代理方法里判断是哪个Alert了 27 if (buttonIndex == 0) { 28 NSLog(@"取消"); 29 } else if (buttonIndex == 1) { 30 NSLog(@"确定"); 31 } 32 }]; 33 34 [alertView show]; 35 } 36 37 @end
其运行效果如下:
当今社会竞争中想要脱颖而出,人必须有一技之长,而且万里挑一。
每个人都有不同的选择,有时一个正确的选择比奋斗本身更重要。
做你喜欢的事情,做你擅长的事情。 没有七十二变,岂能大闹天宫?