ios UIButton事件处理
2019-03-26 16:06
127 查看
创建一个button 并添加对象
[code]//创建一个圆角button UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame=CGRectMake(100, 100, 80, 40); [btn setTitle:@"按钮" forState:UIControlStateNormal]; //向按钮添加事件 //p1 addTarget谁来实现这个事件函数 //p2 @selector(pressBtn) //p3 事件类型 forControlEvents UIControlEventTouchUpInside 事件处理函数类型 //UIControlEventTouchUpInside 点击按钮弹起事件 //UIControlEventTouchDown 当我们的手指触碰到屏幕上时 [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn];
事件实现方法
[code]-(void)pressBtn { NSLog(@"12454"); }
事件 带参数
[code] [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; 实现的方法 //带参数函数 参数为按钮本身 -(void)pressBtn:(UIButton*)btn { NSLog(@"%@",btn); }
[code]// // ViewController.m // les11 // // Created by 易飞 on 2019/3/26. // Copyright © 2019 yifei. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController //创建ui控件 -(void)createRectButton { //创建一个btn对象,根据类型来创建btn //btn类型圆角类型 btn UIButtonTypeRoundedRect //通过类方法来创建 l内存自己管理 UIButton* btn =[UIButton buttonWithT 4000 ype:UIButtonTypeRoundedRect]; //设置btn的位置 btn.frame =CGRectMake(100, 100, 100, 40); //设置按钮的文字内容 //@parameter //p1:字符串类型,显示到按钮上的文字 //p2 设置文字显示的状态类型 UIControlStateNormal 正常状态 UIControlStateHighlighted 高亮状态 [btn setTitle:@"按钮01" forState:UIControlStateNormal]; [btn setTitle:@"按钮按下" forState:UIControlStateHighlighted]; btn.backgroundColor = [UIColor grayColor]; //背景颜色 //设置文字颜色 正常状态下的颜色 p1 颜色 p2状态 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted]; //设置按钮的风格颜色 优先级比setTitleColor 低 [btn setTintColor:[UIColor whiteColor]]; //UILable控件 btn.titleLabel.font = [UIFont systemFontOfSize:30];//设置btn的文字大小 [self.view addSubview:btn];//添加到视图 } -(void)createImageButton { //创建一个自定义类型的button UIButtonTypeCustom UIButton*btnImage = [UIButton buttonWithType:UIButtonTypeCustom]; btnImage.frame= CGRectMake(100, 200, 100, 100); UIImage*icon01 =[UIImage imageNamed:@"btn01"]; UIImage*icon02 = [UIImage imageNamed:@"btn02"]; [btnImage setImage:icon01 forState:UIControlStateNormal]; [btnImage setImage:icon02 forState:UIControlStateHighlighted]; [self.view addSubview:btnImage]; } -(void)createButton { //创建一个圆角button UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame=CGRectMake(100, 100, 80, 40); [btn setTitle:@"按钮" forState:UIControlStateNormal]; //向按钮添加事件 //p1 addTarget谁来实现这个事件函数 //p2 @selector(pressBtn) //p3 事件类型 forControlEvents UIControlEventTouchUpInside 事件处理函数类型 //UIControlEventTouchUpInside 点击按钮弹起事件 //UIControlEventTouchDown 当我们的手指触碰到屏幕上时 [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside]; //触碰时调用事件函数 [btn addTarget:self action:@selector(touhDown) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; UIButton*btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn02.frame= CGRectMake(100, 200, 80, 40); [btn02 setTitle:@"按钮02" forState:UIControlStateNormal]; [btn02 addTarget:self action:@selector(pressBtn02) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn02]; //设置按钮的标记值 btn.tag = 101; btn.tag =102; } -(void)pressBtn02 { NSLog(@"bbbb"); } -(void)touhDown { NSLog(@"触碰"); } //普通函数 -(void)pressBtn { NSLog(@"12454"); } //带参数函数 -(void)pressBtn:(UIButton*)btn { NSLog(@"%@",btn); } - (void)viewDidLoad { [super viewDidLoad]; [self createButton]; } @end
最终版
[code]// // ViewController.m // les11 // // Created by 易飞 on 2019/3/26. // Copyright © 2019 yifei. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController //创建ui控件 -(void)createRectButton { //创建一个btn对象,根据类型来创建btn //btn类型圆角类型 btn UIButtonTypeRoundedRect //通过类方法来创建 l内存自己管理 UIButton* btn =[UIButton buttonWithType:UIButtonTypeRoundedRect]; //设置btn的位置 btn.frame =CGRectMake(100, 100, 100, 40); //设置按钮的文字内容 //@parameter //p1:字符串类型,显示到按钮上的文字 //p2 设置文字显示的状态类型 UIControlStateNormal 正常状态 UIControlStateHighlighted 高亮状态 [btn setTitle:@"按钮01" forState:UIControlStateNormal]; [btn setTitle:@"按钮按下" forState:UIControlStateHighlighted]; btn.backgroundColor = [UIColor grayColor]; //背景颜色 //设置文字颜色 正常状态下的颜色 p1 颜色 p2状态 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted]; //设置按钮的风格颜色 优先级比setTitleColor 低 [btn setTintColor:[UIColor whiteColor]]; //UILable控件 btn.titleLabel.font = [UIFont systemFontOfSize:30];//设置btn的文字大小 [self.view addSubview:btn];//添加到视图 } -(void)createImageButton { //创建一个自定义类型的button UIButtonTypeCustom UIButton*btnImage = [UIButton buttonWithType:UIButtonTypeCustom]; btnImage.frame= CGRectMake(100, 200, 100, 100); UIImage*icon01 =[UIImage imageNamed:@"btn01"]; UIImage*icon02 = [UIImage imageNamed:@"btn02"]; [btnImage setImage:icon01 forState:UIControlStateNormal]; [btnImage setImage:icon02 forState:UIControlStateHighlighted]; [self.view addSubview:btnImage]; } -(void)createButton { //创建一个圆角button UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn.frame=CGRectMake(100, 100, 80, 40); [btn setTitle:@"按钮" forState:UIControlStateNormal]; //向按钮添加事件 //p1 addTarget谁来实现这个事件函数 //p2 @selector(pressBtn) //p3 事件类型 forControlEvents UIControlEventTouchUpInside 事件处理函数类型 //UIControlEventTouchUpInside 点击按钮弹起事件 //UIControlEventTouchDown 当我们的手指触碰到屏幕上时 [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside]; //触碰时调用事件函数 [btn addTarget:self action:@selector(touhDown) forControlEvents:UIControlEventTouchDown]; [self.view addSubview:btn]; UIButton*btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect]; btn02.frame= CGRectMake(100, 200, 80, 40); [btn02 setTitle:@"按钮02" forState:UIControlStateNormal]; [btn02 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn02]; //设置按钮的标记值 btn.tag = 101; btn.tag =102; } -(void)pressBtn02 { NSLog(@"bbbb"); } -(void)touhDown { NSLog(@"触碰"); } //普通函数 -(void)pressBtn { NSLog(@"12454"); } //带参数函数 -(void)pressBtn:(UIButton*)btn { NSLog(@"%@",btn); if(btn.tag==101){ NSLog(@"现在是101"); }else if(btn.tag==102){ NSLog(@"现在是102"); } } - (void)viewDidLoad { [super viewDidLoad]; [self createButton]; } @end
相关文章推荐
- 发现 IOS 如UIButton 事件处理函数参数和不带参数写法不一样
- ios UIButton的点击与松开事件处理
- ios UIButton的点击与松开事件处理
- IOS的基本控件的使用-UIButton的事件处理
- iOS开发笔记--iOS 事件处理机制与图像渲染过程
- iOS UIButton 点击事件带多参数
- ios事件处理读书笔记
- IOS 触摸事件的处理
- 响应者链条,iOS中touchs事件的处理流程。
- iOS UIButton点击事件传递多个参数的解决办法 -----使用关联函数
- iOS Block 处理UITableViewCell上button的点击事件
- iOS自动处理键盘事件的第三方库:IQKeyboardManager
- iOS Programming – 触摸事件处理 (转)
- iOS事件处理——Swift & Objective-C 表述
- 『IOS』再看ios的事件处理
- iOS开发 -- 事件处理(触摸,晃动)
- IOS触摸事件处理(如何处理多个触摸点)
- iOS开发笔记--iOS 事件处理机制与图像渲染过程
- IOS的处理touch事件处理(依照手指的移动移动一个圆,开发环境用的ios7,storyboard)
- IOS 构造和使用TableView(基于storyboard) (二)接收和处理Table View事件