循环按钮,并且选中 (附效果图)
2015-06-10 09:47
225 查看
效果图
以下代码均可全部复制,直接到上图的效果图
#import "ViewController.h"
@interface ViewController ()
//设置属性
@property (nonatomic,strong)NSMutableDictionary *aihao;
@property (nonatomic,strong)UILabel *carheard;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatUI];
}
#define Screen [UIScreen mainScreen]
-(void)creatUI
{
NSArray* citys = @[@"旅游",@"汽车",@"房产",@"美食",@"上网",@"投资",@"体育运动",@"影视",@"电影",@"户外",@"阅读"];
self.aihao = [NSMutableDictionary dictionary];
for (int i = 0; i<citys.count; i++) {
UILabel* carheard;
carheard = [[UILabel alloc]initWithFrame:CGRectMake((i%4)*Screen.bounds.size.width/4+30, (i/4)*Screen.bounds.size.height/15+50 , Screen.bounds.size.width/4, 30)];
UIButton* select = [[UIButton alloc]initWithFrame:CGRectMake(carheard.frame.origin.x-20,carheard.frame.origin.y+8,15,15)];
[select setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[select addTarget:self action:@selector(selectedion:) forControlEvents:UIControlEventTouchUpInside];
[select setBackgroundImage:[UIImage imageNamed:@"xuanze_default_03"] forState:UIControlStateNormal];
[carheard setText:citys[i]];
[carheard setBackgroundColor:[UIColor whiteColor]];
[carheard setFont:[UIFont systemFontOfSize:14]];
[self.view addSubview:carheard];
[self.view addSubview:select];
}
}
-(void)selectedion:(UIButton*)sender{
if(sender.selected)
{
[sender setSelected:NO];
[sender setBackgroundImage:[UIImage imageNamed:@"xuanze_default_03"] forState:UIControlStateNormal];
[self.aihao removeObjectForKey:sender.titleLabel.text];
}else{
[sender setSelected:YES];
[sender setBackgroundImage:[UIImage imageNamed:@"xuanze_hover_69"] forState:UIControlStateNormal];
}
NSLog(@"%@",self.aihao);
}
以下代码均可全部复制,直接到上图的效果图
#import "ViewController.h"
@interface ViewController ()
//设置属性
@property (nonatomic,strong)NSMutableDictionary *aihao;
@property (nonatomic,strong)UILabel *carheard;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self creatUI];
}
#define Screen [UIScreen mainScreen]
-(void)creatUI
{
NSArray* citys = @[@"旅游",@"汽车",@"房产",@"美食",@"上网",@"投资",@"体育运动",@"影视",@"电影",@"户外",@"阅读"];
self.aihao = [NSMutableDictionary dictionary];
for (int i = 0; i<citys.count; i++) {
UILabel* carheard;
carheard = [[UILabel alloc]initWithFrame:CGRectMake((i%4)*Screen.bounds.size.width/4+30, (i/4)*Screen.bounds.size.height/15+50 , Screen.bounds.size.width/4, 30)];
UIButton* select = [[UIButton alloc]initWithFrame:CGRectMake(carheard.frame.origin.x-20,carheard.frame.origin.y+8,15,15)];
[select setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[select addTarget:self action:@selector(selectedion:) forControlEvents:UIControlEventTouchUpInside];
[select setBackgroundImage:[UIImage imageNamed:@"xuanze_default_03"] forState:UIControlStateNormal];
[carheard setText:citys[i]];
[carheard setBackgroundColor:[UIColor whiteColor]];
[carheard setFont:[UIFont systemFontOfSize:14]];
[self.view addSubview:carheard];
[self.view addSubview:select];
}
}
-(void)selectedion:(UIButton*)sender{
if(sender.selected)
{
[sender setSelected:NO];
[sender setBackgroundImage:[UIImage imageNamed:@"xuanze_default_03"] forState:UIControlStateNormal];
[self.aihao removeObjectForKey:sender.titleLabel.text];
}else{
[sender setSelected:YES];
[sender setBackgroundImage:[UIImage imageNamed:@"xuanze_hover_69"] forState:UIControlStateNormal];
}
NSLog(@"%@",self.aihao);
}
相关文章推荐
- Twitter 相关APP开发
- 实现真正的自动化,expect脚本语言使用
- html5常用的属性收集
- iOS学习笔记02——以编码的方式实现Auto Layout自动布局(一)
- JS 函数访问方式总结
- 关于Tcp的一些浅见
- Java基础毕向东day03
- 16款响应式布局框架
- andorid下从相册选取/拍照选取一张相片并剪切
- UESTC 490 Swap Game
- 专题-计算机网络和协议
- 五十八 后记 我在软件园的那些日子里 第一部《职场的温柔》
- 人物动画声音随机播放,资源加载
- Java 返回oracle sys_guid() 乱码问题
- SpringMVC中AJAX请求出现406
- 配置GRE隧道
- Visual C++学习总结——进程或线程与指定CPU绑定
- Asp.Net Url重写页面中Session无效
- LDA主题模型学习笔记3:变分推断(E-step)
- Ubuntu 13.10 下安装伪分布式hbase 0.99.0