iOS block处理button的点击事件

一. 代码
  • .h文件内容

    #import <UIKit/UIKit.h>

    复制代码
      typedef void (^ButtonBlock)(UIButton *);
    
      @interface BlockButton : UIButton
    
      - (void)addTapBlock:(ButtonBlock)block;
    
      @end
  • .m文件内容

    #import "BlockButton.h"

    复制代码
      @interface BlockButton()
    
      @property(nonatomic,copy) ButtonBlock block;
    
      @end
    
      @implementation BlockButton
    
      - (void)addTapBlock:(ButtonBlock)block
      {
          _block = block;
          [self addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
      }
    
      - (void)buttonAction:(UIButton *)button
      {
          _block(button);
      }
    
      @end
相关推荐
韦胖漫谈IT9 小时前
Apple M3 Max 与 Apple M5 Max 对比:本地算力的新旧王者之争
网络·人工智能·macos·transformer
Tyler_19 小时前
iOS PlayWright mcp server
前端·ios·ai编程
greasyfork1 天前
多种数据库管理混乱?Navicat Premium 17 for Mac 统一解决
数据库·mysql·macos·mac
碎_浪1 天前
Mac 壁纸被 MDM 锁住?我做了个 2MB 的开源方案
macos·开源·swift
2501_916008891 天前
iOS 证书管理最佳实践 从创建到续期的完整指南
android·ios·小程序·https·uni-app·iphone·webview
逆向编程1 天前
AI编程CLI两种配置方案(macOS专用)
macos·ai编程
沐禾安信1 天前
开会来不及记录?Mac 录屏方法收好
macos·音视频·电脑录屏
ITKEY_2 天前
macOS brew 安装的nginx 文件在哪里?
运维·nginx·macos
大E帝国子民12 天前
MacOS 安装Seismic Unix
服务器·macos·unix
黑科技iOS上架2 天前
ReactNative入门介绍
经验分享·ios