麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

用block改寫UIButton點擊事件和UIAlerView的按鈕點擊代理方法

2019-11-14 18:20:42
字體:
來源:轉載
供稿:網友
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         // 調用此方法以觸發block22         [self addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];23     }24     return self;25 }26 27 - (void)clickAction:(MyBlockButton *)button28 {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 // 自定義方法,調用block10 - (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 *)button13 {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 *)title11                       message:(NSString *)message12             cancelButtonTitle:(NSString *)cancelButtonTitle13             otherButtonTitles:(NSString *)otherButtonTitles14                 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)buttonIndex17 {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

其運行效果如下:


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: aaaaa国产欧美一区二区 | 亚洲欧美国产高清 | 黄色日韩网站 | 国产一及毛片 | 国产1区2区3区中文字幕 | 欧美成人午夜精品久久久 | 欧美日韩国产中文字幕 | 成人免费网站在线观看视频 | 国产午夜亚洲精品午夜鲁丝片 | 精品国产一区二区三区四区在线 | 色综合网在线观看 | 黄色电影免费网址 | 欧美日韩一区,二区,三区,久久精品 | 日本在线视频免费 | 99re热视频这里只精品 | 久久久久久久久久久久网站 | 北原夏美av | 在线成人毛片 | 黄色片免费在线 | 欧美人人干| 在线看免费观看av | 日本在线视频一区二区三区 | 欧美性激情视频 | 美女黄视频在线观看 | 色视频91 | 性感美女一级毛片 | 日本黄色大片免费 | 精品国产99久久久久久宅男i | 精品亚洲福利一区二区 | 欧美一区二区三区久久精品视 | 免费黄色a| 一级做受毛片免费大片 | 9191久久久久视频 | 亚洲性一区 | 精品亚洲夜色av98在线观看 | 久草在线手机视频 | 国产免费黄色 | 久久精品中文字幕一区二区三区 | 国产一级二级毛片 | 久久精品中文字幕一区二区三区 | 成人毛片视频免费看 |