SoFunction
Updated on 2025-04-12

IOS setOnclick event analysis

In Android, click events are set in the form of setOnclick, which is very convenient to use, while in iOS, it is done in the form of addTarget. Each time you set a click event, you need to declare a new method, which seems a bit troublesome in most cases. And generally speaking, the most common thing we use is the TouchUpInside click event, so for the convenience of use, I have expanded the click event of UIButton/UIView.

use

As before, let's take a look at how to use it after the extension.

oc version

[_btn1 setOnclick:^{
  NSLog(@"click btn1");
}];

Swift version

 {
  print("click btn")
}

It's really easy to use ~

Extension process

Next, let’s take a look at how it expands?

oc version

We just need to add a Category to UIButton and it will be used.

  #import <UIKit/>

  @interface UIButton(click)
  @property (nonatomic, strong) void (^clickBlock) (void);
  - (void) setOnclick : (void (^)(void))block;
  - (void) clickBtn : (UIButton*) sender;
  - (void) setTarget : action:(SEL)action;
  @end
  
  #import "UIButton+"
  #import <objc/>
  
  @implementation UIButton(click)
  
  static void *clickKey = &clickKey;
  - (void)setClickBlock:(void (^)(void))clickBlock{
    objc_setAssociatedObject(self, & clickKey, clickBlock, OBJC_ASSOCIATION_COPY);
  }
  
  - (void (^)(void))clickBlock{
    return objc_getAssociatedObject(self, &clickKey);
  }
  
  -(void)setOnclick:(void (^)(void))block{
     = block;
    [self addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
  }
  
  - (void) clickBtn : (UIButton*) sender{
    ();
  }
  
  @end

If we don't want to import UIButton+ every time, we just need to add UIButton+ to the pch file.

Swift version

Relatively speaking, the swift version is a bit more troublesome. It cannot directly extend the properties of the closure type, so in the end, an additional UIClick object was created.

  class UIClick : Any{
    var click : () -> Void = {return}
  }
  
  extension UIButton : Property{
    var saveClick : UIClick{
      get{
        return get0()
      }
      set{
        return set0(newValue)
      }
    }
    
    func setOnClick(click : @escaping () -> Void) {
       = UIClick()
       = click
      (self, action: #selector(btnClick), for: .touchUpInside)
    }
    
    @objc func btnClick(){
      ()
    }
  }

UIView onClick

After reading the above expansion process, I believe everyone is also aware of the expansion of UIView onClick. The process is basically the same. Next, I will just write a few different parts.

  func setOnClickView(click : @escaping () -> Void) {
     = true
     = UIClick()
     = click
    let tap = UITapGestureRecognizer(target: self, action: #selector(btnClickView))
     = 1
    (tap)
  }

The oc is skipped, the principle is the same, and the code is also very simple.

Summarize

Extension is not complicated, but it does bring a lot of convenience. I hope this expansion idea can make you shine. If there are any errors in the above content, please correct me.