zoukankan      html  css  js  c++  java
  • IOS之UI--自定义按钮实现代理监听点击事件

    前言:

    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

     
     
     
     
     
  • 相关阅读:
    Linux之流程判断
    Linux之Shell变量
    Linux之RAID
    Y-Sport
    [ST2017] Lab1: Triangle type and Junit test
    [ST2017] Hw3: Prime Path
    [ST2017] Hw2: Fault, Error, Failure and test case for projects
    [SPM2017] Hw1: The outcome of my project [Deadline: 23:59:59, Mar.1, 2017]
    [ST2017] Hw1: An error from my past projects [Deadline: 23:59:59, Feb.27, 2017]
    一个使用Jmeter做接口性能测试的实战案例
  • 原文地址:https://www.cnblogs.com/goodboy-heyang/p/4982351.html
Copyright © 2011-2022 走看看