赞
踩
创建一个button 并添加对象
- //创建一个圆角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];
事件实现方法
- -(void)pressBtn
- {
- NSLog(@"12454");
- }
事件 带参数
- [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:btn];
-
- 实现的方法
- //带参数函数 参数为按钮本身
- -(void)pressBtn:(UIButton*)btn
- {
- NSLog(@"%@",btn);
- }
- //
- // 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(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
最终版
- //
- // 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
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。