关于UIButton的事件枚举有许多,平时用的少所以很多的都不是很清楚,今天了解了下,看了以前的代码,觉得在UIButton选中时操作写了许多冗余代码,而忽略了UIButton一个很重要的属性,如下:

  1. typedef NS_OPTIONS(NSUInteger, UIControlState) {
  2. UIControlStateNormal       = 0,
  3. UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
  4. UIControlStateDisabled     = 1 << 1,
  5. UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
  6. #ifndef SDK_HIDE_TIDE
  7. UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // Applicable only when the screen supports focus
  8. #endif
  9. UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
  10. UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
  11. };

中的UIControlStateSelected表示是否选中,NO表示未选中,YES表示选中;

1.这是之前写的:

创建UIButton,通过for 循环去创建

  1. //顶部view的初始化
  2. - (void)initTopView{
  3. topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 40)];
  4. topFrame = topView.frame;
  5. topView.backgroundColor = [UIColor whiteColor];
  6. topView.alpha = .8;
  7. NSArray *titleArr = @[@"人气",@"价格",@"桌数",@"优惠"];
  8. for (int i = 0; i < titleArr.count; i ++) {
  9. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  10. [btn setTitle:titleArr[i] forState:UIControlStateNormal];
  11. [btn setImage:[UIImage imageNamed:@"ph"] forState:UIControlStateNormal];
  12. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  13. [btn setBackgroundImage:[UIImage imageNamed:@"button1"] forState:UIControlStateNormal];
  14. btn.titleLabel.font = [UIFont systemFontOfSize:12];
  15. btn.showsTouchWhenHighlighted = YES;
  16. btn.frame = CGRectMake(10 + i * ((kScreen_width - 50)/4 + 10) , 10, (kScreen_width - 50)/4, 25);
  17. //设置tag值
  18. btn.tag = i + 100;
  19. [btn addTarget:self action:@selector(choose:) forControlEvents:UIControlEventTouchUpInside];
  20. [topView addSubview:btn];
  21. }
  22. [self.view addSubview:topView];
  23. }

添加响应事件:

  1. //人气、价格、作品数、优惠
  2. - (void)choose:(UIButton *)sender{
  3. for (int i = 0; i < 4; i++) {
  4. UIButton *btn = (UIButton *)[[sender superview]viewWithTag:100 + i];
  5. [btn setSelected:NO];
  6. if (!btn.selected) {
  7. [btn setImage:[UIImage imageNamed:@"ph"] forState:UIControlStateNormal];
  8. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  9. [btn setBackgroundImage:[UIImage imageNamed:@"button1"] forState:UIControlStateNormal];
  10. }
  11. }
  12. UIButton *button = (UIButton *)sender;
  13. [button setSelected:YES];
  14. [button setImage:[UIImage imageNamed:@"pho"] forState:UIControlStateNormal];
  15. [button setTitleColor:[UIColor colorWithRed:170.0/255 green:107.0/255 blue:208.0/255 alpha:1] forState:UIControlStateNormal];
  16. [button setBackgroundImage:[UIImage imageNamed:@"button2"] forState:UIControlStateNormal];
  17. }

这种是最简单的,相对也是最繁琐的,多了很多不必要的冗余代码,下面就让我们看看改进的;

2.通过使用UIButton自己的一个selected属性和normal属性重构的,如下所示

  1. -(void)initUIButtonView{
  2. _titleArr = @[@"人气",@"价格",@"桌数",@"优惠"];
  3. for (int i = 0; i < _titleArr.count; i ++) {
  4. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
  5. btn.frame = CGRectMake(10 + i * ((kScreen_width - 50)/4 + 10) , 20, (kScreen_width - 50)/4, 25);
  6. [btn setTitle:_titleArr[i] forState:UIControlStateNormal];
  7. btn.titleLabel.font = [UIFont systemFontOfSize:12];
  8. btn.showsTouchWhenHighlighted = YES;
  9. //设置tag值
  10. btn.tag = i + 100;
  11. btn.selected = NO;
  12. [btn addTarget:self action:@selector(choose:) forControlEvents:UIControlEventTouchUpInside];
  13. [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
  14. [btn setImage:[UIImage imageNamed:@"ph"] forState:UIControlStateNormal];
  15. [btn setBackgroundImage:[UIImage imageNamed:@"button1"] forState:UIControlStateNormal];
  16. [btn setTitleColor:[UIColor colorWithRed:170.0/255 green:107.0/255 blue:208.0/255 alpha:1] forState:UIControlStateSelected];
  17. [btn setImage:[UIImage imageNamed:@"pho"] forState:UIControlStateSelected];
  18. [btn setBackgroundImage:[UIImage imageNamed:@"button2"] forState:UIControlStateSelected];
  19. [self.view addSubview:btn];
  20. }
  21. }

在创建的时候就给定了正常时候UIButton的样式,和选中UIButton时的按钮颜色,注意这里设置了默认的selected = NO;和UIControlStateSelected

在给定按钮选择事件,设置对应selected的状态值,如下所示:

  1. //人气、价格、作品数、优惠
  2. - (void)choose:(UIButton *)sender{
  3. for (int i = 0; i < _titleArr.count; i++) {
  4. UIButton *btn = (UIButton *)[[sender superview]viewWithTag:100 + i];
  5. [btn setSelected:NO];
  6. }
  7. UIButton *button = (UIButton *)sender;
  8. [button setSelected:YES];
  9. }

这样看上去,第二种方法,是不是比第一种方法更简单明了,去除了相关的冗余代码的,效果如下所示

注:改进,上面的我们可以在不同的按钮上面切换状态,但在同一个按钮上面点击多次状态不会改变,针对上述问题做了些许的改动,其实主要是在点击事件里面,判断当前按钮的状态去改变,代码如下:

  1. //人气、价格、作品数、优惠
  2. - (void)choose:(UIButton *)sender{
  3. for (int i = 0; i < _titleArr.count; i++) {
  4. UIButton *btn = (UIButton *)[[sender superview]viewWithTag:100 + i];
  5. //选中当前按钮时
  6. if (sender.tag == btn.tag) {
  7. sender.selected = !sender.selected;
  8. }else{
  9. [btn setSelected:NO];
  10. }
  11. }
  12. }

效果图如下所示:

ios中UIButton选中状态切换的更多相关文章

  1. iOS UIButton选中状态切换

    UIButton*payBtn = [UIButtonbuttonWithType:UIButtonTypeCustom]; payBtn.frame=CGRectMake(size.width-24 ...

  2. iOS中UIButton控件的用法及部分参数解释

    在UI控件中UIButton是极其常用的一类控件,它的类对象创建与大多数UI控件使用实例方法init创建不同,通常使用类方法创建: + (id)buttonWithType:(UIButtonType ...

  3. iOS 中各种横竖屏切换总结

    iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结.注意 横屏两种情况是反的你知道吗? UI ...

  4. iOS 中UIButton的 settitle 和 titlelabel的使用误区

    UIButton中设置Titl方法包括以下几种: - (void)setTitle:(NSString *)title forState:(UIControlState)state; - (void) ...

  5. IOS中UIButton和UIImageView的区别

    1.使用场合 UIImageView:如果仅仅是为了显示图片,不需要监听图片的点击事件 UIButton:既要显示图片,又要监听图片等点击事件 2.相同点 都能显示图片 3.不同点 UIButton能 ...

  6. Js获取Gridview中Dropdownlist选中状态

    在Gridview中加入Dropdownlist模板列,加入DropDownlist 是一种常用的操作,其中涉及到如何获取选择项和Gridview重新绑定两个要点. 如图 前台代码如下 <%@ ...

  7. html li css选中状态切换

    思路:点击当前li元素后是用removeClass()删除所有兄弟元素(使用siblings()获取)的class样式,然后使用addClass()为当前li添加class. 具体演示如下: 1.HT ...

  8. iOS开发 tabBarController选中状态

    self.tabBarController.selectedIndex = 0;  // 默认是0:

  9. ios 清除列表选中状态

    [tableView deselectRowAtIndexPath:indexPath animated:YES];

随机推荐

  1. poj3321

    树映射到树状数组上 非常好的题目,给了我很多启发 题目要求动态求一个棵子树的节点个数 不禁联想到了前缀和,只要我们能用一个合适的优先级表示每个顶点,那么就好做了 我们可以考虑将子树表示成区间的形式 这 ...

  2. HNOI2008Cards

    看了一下polya和burnside定理,感觉还行(就是不会证……) 这题用的是burnside ans=在每个置换群下不动的方案数之和除以置换数 这题有个难点在取模 关于对p(p为素数)取模(涉及到 ...

  3. Bsie(鄙视IE)

    http://www.bootcss.com/p/bsie/ 欢迎,这是bsie项目主页. 简介 bsie弥补了Bootstrap对IE6的不兼容.Bootstrap是 twitter.com 推出的 ...

  4. 各种兼容的placeholder

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  5. NK 1137: 石子合并问题

     1137: 石子合并问题 Time Limit: 1500 ms    Memory Limit: 10000 kB    Judge type: Multi-cases Total Submit ...

  6. 【转】NDK上建立自己的项目

    原文网址:http://www.cnblogs.com/sardine/archive/2011/07/30/2121845.html 建立Android.mk文件 ~/android-ndk/app ...

  7. Nginx出现“413 Request Entity Too Large”错误解决方法

    Nginx出现“413 Request Entity Too Large”错误解决方法 2011-03-25 13:49:55|  分类: 默认分类 |  标签:413  request  entit ...

  8. dojo(五):Dijit-基本组件

    转自:http://blog.csdn.net/trendgrucee/article/details/12679949 1.简介 Dijit是Dojo的UI框架,包含一系列丰富的组件以帮助你快速开发 ...

  9. 工作流设计 zt

    工作流设计 业务流程管理模块是本平台的重要组成部分,要实现将已经发布的标准中规范化的流程转化为具体计算机中的流程从而实现流程的自动运转,将标准化成果与员工的日常工作紧密结合起来,具有重要意义. 业务流 ...

  10. Android中Bitmap和Drawable,等相关内容

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...