前言:
Objective-C提供的按鈕監聽事件的方法是
不含參數的監聽方法
[button實例對象 addTarget:self action:@selector(func) forControlEvents:UIControlEventTouchUpInside];
含參數的監聽方法
[button實例對象 addTarget:self action:@selector(func:) forControlEvents:UIControlEventTouchUpInside];
下面通過代理的方式,將這兩個監聽事件的方法在自定義UIButton中封裝起來,然后使用的時候,就類似Java的事件監聽。
自定義按鈕實現代理監聽點擊事件
因為有兩種不同的監聽方法,一個不含參數,一個含參數,所以最好用兩個代理協議來處理,一個協議一個行為業務方法:
ButtonDelegate.h
1 #import <Foundation/Foundation.h> 2 3 @protocol ButtonDelegate <NSObject> 4 5 @required 6 7 /** 8 * 不含參數的事件監聽方法 9 */ 10 -(void)delegateFunction; 11 12 13 @end
ButtonDelegateWithParameter.h
1 #import <Foundation/Foundation.h> 2 3 @protocol ButtonDelegateWithParameter <NSObject> 4 5 6 /** 7 * 含參數的事件監聽方法 8 */ 9 -(void)delegateFunctionWithParameter:(id)parameter; 10 11 @end
然后自定義UIbutton,並在自定義UIbutton中組合兩個對應的代理delegate的引用。
HQButton.h
1 #import <UIKit/UIKit.h> 2 #import "ButtonDelegate.h" 3 #import "ButtonDelegateWithParameter.h" 4 5 @interface HQButton : UIButton 6 7 /** 代理 */ 8 @property (nonatomic,weak)id<ButtonDelegate> delegate; 9 10 /** 含參數代理 */ 11 @property (nonatomic,weak)id<ButtonDelegateWithParameter> delegateWithParamater; 12 13 @end
HQButton.m
1 #import "HQButton.h" 2 3 @implementation HQButton 4 5 /** 6 * 懶加載的使用,在需要監聽代理的時候,所以只需要重寫set方法,然后在set方法中實現加載delegate 7 * 亮點:就是重寫set方法內部實現addTarget方法,監聽self的func,然后在func內部調用delegate的實現協議的方法 8 * @return void 9 */ 10 -(void)setDelegate:(id<ButtonDelegate>)delegate 11 { 12 [self addTarget:self action:@selector(func) forControlEvents:UIControlEventTouchUpInside]; 13 _delegate = delegate; 14 15 } 16 -(void)setDelegateWithParamater:(id<ButtonDelegateWithParameter>)delegateWithParamater{ 17 [self addTarget:self action:@selector(funcWithParameter:) forControlEvents:UIControlEventTouchUpInside]; 18 _delegateWithParamater = delegateWithParamater; 19 } 20 21 -(void)func 22 { 23 [self.delegate delegateFunction]; 24 } 25 -(void)funcWithParameter:(id)parameter 26 { 27 [self.delegateWithParamater delegateFunctionWithParameter:parameter]; 28 } 29 @end
在ViewController中實現相關的協議,然后使用這個自定義button然后添加delegate。
1 #import "ViewController.h" 2 #import "HQButton.h" 3 4 @interface ViewController ()<ButtonDelegate,ButtonDelegateWithParameter> 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 13 //============創建自定義按鈕================ 14 HQButton* button = [[HQButton alloc] init]; 15 16 //添加含參數的代理 17 button.delegateWithParamater = self; 18 //設置按鈕的位置,背景顏色,顯示文字 19 button.frame = CGRectMake(100, 100, 200, 100); 20 button.backgroundColor = [UIColor redColor]; 21 22 //=============為按鈕添加代理============== 23 //添加不含參數的代理 24 button.delegate = self; 25 26 //父控件添加這個按鈕 27 [self.view addSubview:button]; 28 29 } 30 31 - (void)didReceiveMemoryWarning { 32 [super didReceiveMemoryWarning]; 33 34 } 35 //=============實現協議里的方法============== 36 -(void)delegateFunction{ 37 NSLog(@"Hello"); 38 } 39 40 -(void)delegateFunctionWithParameter:(id)parameter{ 41 NSLog(@"self: %@",parameter); 42 } 43 44 @end
源代碼百度雲下載鏈接: http://pan.baidu.com/s/1mgIpuPy 密碼: 89ww