//给按钮设置图片
[b setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
[b addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:b];
[self setAllBtnEnable:false];
UIButton *b1 =[UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img1 = [UIImage imageNamed:@"1_normal.png"];
NSString *path1 = [[NSBundle mainBundle] pathForResource:@"1_normal" ofType:@"png"];
img1 = [UIImage imageWithContentsOfFile:path1];
b1.frame = CGRectMake(120, 20,img1.size.width, img1.size.height);
b1.tag = 101;
[b1 setImage:img1 forState:UIControlStateNormal];
[b1 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
UIButton *b2 = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img2 = [UIImage imageNamed:@"2_normal.png"];
NSString * path2 =[[NSBundle mainBundle] pathForResource:@"2_normal" ofType:@"png"];
img2 =[UIImage imageWithContentsOfFile:path2];
b2.frame = CGRectMake(120, 100, img2.size.width, img2.size.height);
[b2 setImage:img2 forState:UIControlStateNormal];
b2.tag = 102;
[b2 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
UIButton *b3 =[UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img3 = [UIImage imageNamed:@"3_normal.png"];
NSString *path3 = [[NSBundle mainBundle] pathForResource:@"3_normal" ofType:@"png"];
img3 =[UIImage imageWithContentsOfFile:path3];
b3.frame = CGRectMake(120, 180, img3.size.width, img3.size.height);
[b3 setImage:img3 forState:UIControlStateNormal];
b3.tag = 103;
[b3 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
UIButton *b4 = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img4 = [UIImage imageNamed:@"4_normal.png"];
NSString *path4 =[[NSBundle mainBundle] pathForResource:@"4_normal" ofType:@"png"];
img4 = [UIImage imageWithContentsOfFile:path4];
b4.frame =CGRectMake(120, 260, img4.size.width, img4.size.height);
[b4 setImage:img4 forState:UIControlStateNormal];
b4.tag = 104;
[b4 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
UIButton *b5 = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img5 = [UIImage imageNamed:@"5_normal.png"];
NSString *path5 = [[NSBundle mainBundle] pathForResource:@"5_normal" ofType:@"png"];
img5 = [UIImage imageWithContentsOfFile:path5];
b5.frame = CGRectMake(120, 340, img5.size.width, img5.size.height);
[b5 setImage:img5 forState:UIControlStateNormal];
b5.tag = 105;
[b5 addTarget:self action:@selector(click1:) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:b1];
[self.window addSubview:b2];
[self.window addSubview:b3];
[self.window addSubview:b4];
[self.window addSubview:b5];
//父视图中获取所有子视图
NSLog(@"%d,%@",self.window.subviews.count,self.window.subviews);
return YES;
}
int preTag = 0;
-(void)click1:(UIButton *)b
{
NSLog(@"%d",b.tag-100);
for (int i = 1; i<6; i++) {
UIButton *button = (UIButton *)[self.window viewWithTag:100+i ];
if (button.tag == b.tag) {
[b setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d_selected.png",i]] forState:UIControlStateNormal];
}else{
[button setImage:[UIImage imageNamed:[NSString stringWithFormat:@"%d_normal.png",i]] forState:UIControlStateNormal];
}
}
}
-(void)click:(UIButton *)b
{
if (flag) {
[b setImage:[UIImage imageNamed:@"checked.png"] forState:UIControlStateNormal];
}
else{
[b setImage:[UIImage imageNamed:@"unchecked.png"] forState:UIControlStateNormal];
}
[self setAllBtnEnable:flag];
flag = !flag;
}
-(void)setAllBtnEnable:(BOOL)state
{
for(UIButton *b in self.window.subviews){
if (b.tag !=100) {
b.enabled = state;
}
}
}