iOS的UIButton单选的最佳方式
2016-05-10 17:30
369 查看
#import "TestViewController.h" #import "UIImage+Mycategory.h" @interface TestViewController () @property (nonatomic,strong) UIButton *myButton; @end @implementation TestViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor whiteColor]; NSArray *btnArray = @[@"颜色",@"尺寸",@"空间",@"备注"]; for (int i = 0 ; i < btnArray.count; i ++) { float width = (self.view.frame.size.width - 20 - 15)/4; self.myButton = [[UIButton alloc]initWithFrame:CGRectMake(10 + (width + 5) * i, 100, width , 30)]; [_myButton setBackgroundImage:[UIImage imageWithColor:[UIColor lightGrayColor]] forState:UIControlStateNormal]; [_myButton setBackgroundImage:[UIImage imageWithColor:[UIColor orangeColor]] forState:UIControlStateSelected]; [_myButton setTitle:btnArray[i] forState:UIControlStateNormal]; _myButton.tag = 100 + i; [_myButton addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside]; if (i == 0) _myButton.selected = YES;else _myButton.selected = NO; [self.view addSubview:_myButton]; } } - (void)clickAction:(UIButton *)button { if (button.tag != 100) { UIButton *tempBtn = [self.view viewWithTag:100]; if (tempBtn.selected == YES) tempBtn.selected = NO; } if (button != self.myButton) { self.myButton.selected = NO; self.myButton = button; } self.myButton.selected = YES; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
相关文章推荐
- SoapUI测试接口
- NSOperationqueue的一些感悟
- String、StringBuffer与StringBuilder之间区别
- 【BZOJ-1568】Blue Mary开公司 李超线段树 (标记永久化)
- IQueryable & IEnumberable 区别
- String、StringBuffer与StringBuilder之间区别
- 安卓UI控件拖拽的几种方法
- UITableView的两种重用Cell方法的区别
- 关于UIKIT_EXTERN
- No 'Access-Control-Allow-Origin' header is present on the requested resource.
- NGUI-Sprite扩展镜像功能
- 内外分离接口依赖及UIScrollView知识点
- NGUI中不规则形状点击事件
- An error occurred while installing sqlite3 (1.3.11), and Bundler cannot continue
- UINavigationBar透明设置
- An error occurred while installing pcaprub (0.12.0), and Bundler cannot continue
- 获取GUID的方法
- django views中提示cannot convert dictionary update sequence element #0 to a sequence错误
- 自定义collectionViewFlowLayout让UICollectionView的item始终优先居左
- ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别