iOS开发封装带有Button的UIView控件,使用block给button添加点击事件
来源:互联网 发布:php include path 编辑:程序博客网 时间:2024/05/01 02:01
封装的BaseView的.h文件
#import <UIKit/UIKit.h>typedef void (^ButtonBlock) (id sender);@interface BaseView : UIView- (void)addButtonAction:(ButtonBlock)block;@end
封装的BaseView的.m文件
#import "BaseView.h"@interface BaseView ()@property (nonatomic, strong, nullable) ButtonBlock block;@property (nonatomic, strong, nullable) UIButton *button;@end@implementation BaseView- (instancetype)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { self.button = [UIButton buttonWithType:(UIButtonTypeCustom)]; self.button.frame = CGRectMake(0, 0, 100, 100); self.button.backgroundColor = [UIColor blackColor]; [self addSubview:self.button]; [self.button addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)]; } return self;}//实现block回调的方法- (void)addButtonAction:(ButtonBlock)block { self.block = block;}- (void)buttonAction { if (self.block) { self.block(self); }}@end
在viewController中使用BaseView
#import "ViewController.h"#import "BaseView.h"@interface ViewController ()@property (nonatomic, strong, nullable) BaseView *baseView;@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; self.baseView = [[BaseView alloc] init]; self.baseView.frame = self.view.frame; [self.view addSubview:self.baseView]; [self.baseView addButtonAction:^(id sender) { NSLog(@"呵呵呵"); }];}@end
看看是不是点击button就能打印“呵呵呵”了呢
2 0
- iOS开发封装带有Button的UIView控件,使用block给button添加点击事件
- iOS开发封装带有Button的UIView控件,使用代理给button添加点击事件
- iOS开发封装带有Button的UIView控件,使用target-action模式给button添加点击事件
- iOS,OC给button添加block点击事件扩展
- 封装-给继承自UIView的控件添加点击事件
- 怎么用block给Button和AlertView添加点击事件
- iOS给UIView添加延展使用与Button、uiview。。。
- iOS Block 处理UITableViewCell上button的点击事件
- 给button添加点击事件,打开一个新的窗体
- python 批量添加的button 使用同一点击事件
- Button的点击事件
- Button的点击事件
- Button的点击事件
- Button的点击事件
- IOS界面开发给UIView添加事件
- Button的使用(三):处理点击事件
- iOS button点击事件问题
- ListView中添加Button后,Button的点击事件和ListView的点击事件冲突
- GPU 编程与CG 语言之阳春白雪下里巴人——CG学习读书笔记之数学函数(之一)。
- 焦点轮播图的制作
- 根据黑客的业务逻辑:最牛黑客的外号应该叫“向日葵”
- 【putty】putty、psftp、pscp【原创】
- display:table-cell的几个应用
- iOS开发封装带有Button的UIView控件,使用block给button添加点击事件
- 内部类,类修饰符,上转型对象,多态,接口回调
- 生成二维码
- C语言学习新手常见问题
- NSGA-ⅡMATLAB代码(转载)
- java--继承
- java虚拟机运行机制
- plsql配置
- 设计模式之观察者模式