当前位置:   article > 正文

ios UIButton事件处理_ios uibutton uicontroleventtouchupinside

ios uibutton uicontroleventtouchupinside

创建一个button 并添加对象

  1. //创建一个圆角button
  2. UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  3. btn.frame=CGRectMake(100, 100, 80, 40);
  4. [btn setTitle:@"按钮" forState:UIControlStateNormal];
  5. //向按钮添加事件
  6. //p1 addTarget谁来实现这个事件函数
  7. //p2 @selector(pressBtn)
  8. //p3 事件类型 forControlEvents UIControlEventTouchUpInside 事件处理函数类型
  9. //UIControlEventTouchUpInside 点击按钮弹起事件
  10. //UIControlEventTouchDown 当我们的手指触碰到屏幕上时
  11. [btn addTarget:self action:@selector(pressBtn) forControlEvents:UIControlEventTouchUpInside];
  12. [self.view addSubview:btn];

事件实现方法

  1. -(void)pressBtn
  2. {
  3. NSLog(@"12454");
  4. }

事件 带参数

  1. [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
  2. [self.view addSubview:btn];
  3. 实现的方法
  4. //带参数函数 参数为按钮本身
  5. -(void)pressBtn:(UIButton*)btn
  6. {
  7. NSLog(@"%@",btn);
  8. }
  1. //
  2. // ViewController.m
  3. // les11
  4. //
  5. // Created by 易飞 on 2019/3/26.
  6. // Copyright © 2019 yifei. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. @interface ViewController ()
  10. @end
  11. @implementation ViewController
  12. //创建ui控件
  13. -(void)createRectButton
  14. {
  15. //创建一个btn对象,根据类型来创建btn
  16. //btn类型圆角类型 btn UIButtonTypeRoundedRect
  17. //通过类方法来创建 l内存自己管理
  18. UIButton* btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
  19. //设置btn的位置
  20. btn.frame =CGRectMake(100, 100, 100, 40);
  21. //设置按钮的文字内容
  22. //@parameter
  23. //p1:字符串类型,显示到按钮上的文字
  24. //p2 设置文字显示的状态类型 UIControlStateNormal 正常状态 UIControlStateHighlighted 高亮状态
  25. [btn setTitle:@"按钮01" forState:UIControlStateNormal];
  26. [btn setTitle:@"按钮按下" forState:UIControlStateHighlighted];
  27. btn.backgroundColor = [UIColor grayColor]; //背景颜色
  28. //设置文字颜色 正常状态下的颜色 p1 颜色 p2状态
  29. [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  30. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
  31. //设置按钮的风格颜色 优先级比setTitleColor 低
  32. [btn setTintColor:[UIColor whiteColor]];
  33. //UILable控件
  34. btn.titleLabel.font = [UIFont systemFontOfSize:30];//设置btn的文字大小
  35. [self.view addSubview:btn];//添加到视图
  36. }
  37. -(void)createImageButton
  38. {
  39. //创建一个自定义类型的button UIButtonTypeCustom
  40. UIButton*btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
  41. btnImage.frame= CGRectMake(100, 200, 100, 100);
  42. UIImage*icon01 =[UIImage imageNamed:@"btn01"];
  43. UIImage*icon02 = [UIImage imageNamed:@"btn02"];
  44. [btnImage setImage:icon01 forState:UIControlStateNormal];
  45. [btnImage setImage:icon02 forState:UIControlStateHighlighted];
  46. [self.view addSubview:btnImage];
  47. }
  48. -(void)createButton
  49. {
  50. //创建一个圆角button
  51. UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  52. btn.frame=CGRectMake(100, 100, 80, 40);
  53. [btn setTitle:@"按钮" forState:UIControlStateNormal];
  54. //向按钮添加事件
  55. //p1 addTarget谁来实现这个事件函数
  56. //p2 @selector(pressBtn)
  57. //p3 事件类型 forControlEvents UIControlEventTouchUpInside 事件处理函数类型
  58. //UIControlEventTouchUpInside 点击按钮弹起事件
  59. //UIControlEventTouchDown 当我们的手指触碰到屏幕上时
  60. [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
  61. //触碰时调用事件函数
  62. [btn addTarget:self action:@selector(touhDown) forControlEvents:UIControlEventTouchDown];
  63. [self.view addSubview:btn];
  64. UIButton*btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  65. btn02.frame= CGRectMake(100, 200, 80, 40);
  66. [btn02 setTitle:@"按钮02" forState:UIControlStateNormal];
  67. [btn02 addTarget:self action:@selector(pressBtn02) forControlEvents:UIControlEventTouchUpInside];
  68. [self.view addSubview:btn02];
  69. //设置按钮的标记值
  70. btn.tag = 101;
  71. btn.tag =102;
  72. }
  73. -(void)pressBtn02
  74. {
  75. NSLog(@"bbbb");
  76. }
  77. -(void)touhDown
  78. {
  79. NSLog(@"触碰");
  80. }
  81. //普通函数
  82. -(void)pressBtn
  83. {
  84. NSLog(@"12454");
  85. }
  86. //带参数函数
  87. -(void)pressBtn:(UIButton*)btn
  88. {
  89. NSLog(@"%@",btn);
  90. }
  91. - (void)viewDidLoad {
  92. [super viewDidLoad];
  93. [self createButton];
  94. }
  95. @end

最终版

  1. //
  2. // ViewController.m
  3. // les11
  4. //
  5. // Created by 易飞 on 2019/3/26.
  6. // Copyright © 2019 yifei. All rights reserved.
  7. //
  8. #import "ViewController.h"
  9. @interface ViewController ()
  10. @end
  11. @implementation ViewController
  12. //创建ui控件
  13. -(void)createRectButton
  14. {
  15. //创建一个btn对象,根据类型来创建btn
  16. //btn类型圆角类型 btn UIButtonTypeRoundedRect
  17. //通过类方法来创建 l内存自己管理
  18. UIButton* btn =[UIButton buttonWithType:UIButtonTypeRoundedRect];
  19. //设置btn的位置
  20. btn.frame =CGRectMake(100, 100, 100, 40);
  21. //设置按钮的文字内容
  22. //@parameter
  23. //p1:字符串类型,显示到按钮上的文字
  24. //p2 设置文字显示的状态类型 UIControlStateNormal 正常状态 UIControlStateHighlighted 高亮状态
  25. [btn setTitle:@"按钮01" forState:UIControlStateNormal];
  26. [btn setTitle:@"按钮按下" forState:UIControlStateHighlighted];
  27. btn.backgroundColor = [UIColor grayColor]; //背景颜色
  28. //设置文字颜色 正常状态下的颜色 p1 颜色 p2状态
  29. [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
  30. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
  31. //设置按钮的风格颜色 优先级比setTitleColor 低
  32. [btn setTintColor:[UIColor whiteColor]];
  33. //UILable控件
  34. btn.titleLabel.font = [UIFont systemFontOfSize:30];//设置btn的文字大小
  35. [self.view addSubview:btn];//添加到视图
  36. }
  37. -(void)createImageButton
  38. {
  39. //创建一个自定义类型的button UIButtonTypeCustom
  40. UIButton*btnImage = [UIButton buttonWithType:UIButtonTypeCustom];
  41. btnImage.frame= CGRectMake(100, 200, 100, 100);
  42. UIImage*icon01 =[UIImage imageNamed:@"btn01"];
  43. UIImage*icon02 = [UIImage imageNamed:@"btn02"];
  44. [btnImage setImage:icon01 forState:UIControlStateNormal];
  45. [btnImage setImage:icon02 forState:UIControlStateHighlighted];
  46. [self.view addSubview:btnImage];
  47. }
  48. -(void)createButton
  49. {
  50. //创建一个圆角button
  51. UIButton*btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  52. btn.frame=CGRectMake(100, 100, 80, 40);
  53. [btn setTitle:@"按钮" forState:UIControlStateNormal];
  54. //向按钮添加事件
  55. //p1 addTarget谁来实现这个事件函数
  56. //p2 @selector(pressBtn)
  57. //p3 事件类型 forControlEvents UIControlEventTouchUpInside 事件处理函数类型
  58. //UIControlEventTouchUpInside 点击按钮弹起事件
  59. //UIControlEventTouchDown 当我们的手指触碰到屏幕上时
  60. [btn addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
  61. //触碰时调用事件函数
  62. [btn addTarget:self action:@selector(touhDown) forControlEvents:UIControlEventTouchDown];
  63. [self.view addSubview:btn];
  64. UIButton*btn02 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  65. btn02.frame= CGRectMake(100, 200, 80, 40);
  66. [btn02 setTitle:@"按钮02" forState:UIControlStateNormal];
  67. [btn02 addTarget:self action:@selector(pressBtn:) forControlEvents:UIControlEventTouchUpInside];
  68. [self.view addSubview:btn02];
  69. //设置按钮的标记值
  70. btn.tag = 101;
  71. btn.tag =102;
  72. }
  73. -(void)pressBtn02
  74. {
  75. NSLog(@"bbbb");
  76. }
  77. -(void)touhDown
  78. {
  79. NSLog(@"触碰");
  80. }
  81. //普通函数
  82. -(void)pressBtn
  83. {
  84. NSLog(@"12454");
  85. }
  86. //带参数函数
  87. -(void)pressBtn:(UIButton*)btn
  88. {
  89. NSLog(@"%@",btn);
  90. if(btn.tag==101){
  91. NSLog(@"现在是101");
  92. }else if(btn.tag==102){
  93. NSLog(@"现在是102");
  94. }
  95. }
  96. - (void)viewDidLoad {
  97. [super viewDidLoad];
  98. [self createButton];
  99. }
  100. @end

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号