您的位置:首页 > 其它

多个点击按钮事件 老大教的

2013-07-04 11:38 148 查看
-(void)createButton
{

NSArray *datarr=[NSArray
arrayWithObjects:@"周一",@"周二",@"周三",@"周四",@"周五",
nil];

NSArray *shuziarr=[NSArray
arrayWithObjects:@"4和9",@"5和0",@"1和6",@"2和7",@"3和5",
nil];

for (int i=0; i<5; i++) {

UIButton *btn=[UIButton
buttonWithType:UIButtonTypeCustom];
btn.frame=CGRectMake(7+i*62,
3, 60,
60);

// btn.tintColor=[UIColor blueColor];

btn.tag=i+100;//每个东西
只会有一个tag值

[btn setImage:[UIImage
imageNamed:@"u34_normal"]
forState:UIControlStateNormal];

[btn addTarget:self
action:@selector(pressShou:)
forControlEvents:UIControlEventTouchUpInside];
[self.view
addSubview:btn];

UILabel *data=[[UILabel
alloc] initWithFrame:CGRectMake(0,
0, 60,
30)];
data.tag=i+200;//tag值
都不可以设置重复的

data.textAlignment=NSTextAlignmentCenter;
data.text=[datarr
objectAtIndex:i];
data.font=[UIFont
systemFontOfSize:15.0f];

data.backgroundColor=[UIColor
clearColor];
[btn
addSubview:data];
[data
release];

UILabel *shuzi=[[UILabel
alloc] initWithFrame:CGRectMake(0,
30, 60,
30)];

shuzi.textAlignment=NSTextAlignmentCenter;
shuzi.text=[shuziarr
objectAtIndex:i];
shuzi.tag=i+300;
shuzi.font=[UIFont
systemFontOfSize:15.0f];

shuzi.backgroundColor=[UIColor
clearColor];
[btn
addSubview:shuzi];
[shuzi
release];

}

/*

for (int i=0; i<5; i++) {

UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"u34_normal"]];

image.userInteractionEnabled=YES;

image.frame=CGRectMake(7+i*62, 3, 60, 60);

[self.view addSubview:image];

[image release];

UILabel *data=[[UILabel alloc] initWithFrame:CGRectMake(10, 3, 40, 30)];

data.textAlignment=NSTextAlignmentCenter;

data.text=[datarr objectAtIndex:i];

data.font=[UIFont systemFontOfSize:15.0f];

[image addSubview:data];

[data release];

UILabel *shuzi=[[UILabel alloc] initWithFrame:CGRectMake(10, 30, 40, 20)];

shuzi.textAlignment=NSTextAlignmentCenter;

shuzi.text=[shuziarr objectAtIndex:i];

shuzi.font=[UIFont systemFontOfSize:15.0f];

[image addSubview:shuzi];

[shuzi release];

}*/
}

-(void)pressShou:(UIButton*)btn
{

for (int i =
0; i < 5; i++) {

UILabel *data=(UILabel *)[self.view
viewWithTag:200+i];

UILabel *shuzi=(UILabel *)[self.view
viewWithTag:300+i];

if (btn.tag != i+100) {
data.backgroundColor=[UIColor
clearColor];

shuzi.backgroundColor=[UIColor
clearColor];
shuzi.textColor=Black1;
data.textColor=Black1;

}

else {

shuzi.backgroundColor=[UIColor
blueColor];
data.backgroundColor=[UIColor
blueColor];
shuzi.textColor=white1;
data.textColor=white1;
data.font=[UIFont
boldSystemFontOfSize:15.0f];
shuzi.font=[UIFont
boldSystemFontOfSize:15.0f];

}
}

return;

// if (btn.tag==100) {

// UILabel *data=(UILabel *)[self.view viewWithTag:200];

// UILabel *shuzi=(UILabel *)[self.view viewWithTag:300];

// data.backgroundColor=[UIColor blueColor];

//

// shuzi.backgroundColor=[UIColor blueColor];

// shuzi.textColor=white1;

// data.textColor=white1;

//

//

// }

// else if(btn.tag==101){

// UILabel *data=(UILabel *)[self.view viewWithTag:201];

// UILabel *shuzi=(UILabel *)[self.view viewWithTag:301];

// data.backgroundColor=[UIColor blueColor];

// shuzi.backgroundColor=[UIColor blueColor];

// shuzi.textColor=white1;

// data.textColor=white1;

//

//

// }

// else if(btn.tag==102){

// UILabel *data=(UILabel *)[self.view viewWithTag:202];

// UILabel *shuzi=(UILabel *)[self.view viewWithTag:302];

// data.backgroundColor=[UIColor blueColor];

// shuzi.backgroundColor=[UIColor blueColor];

// shuzi.textColor=white1;

// data.textColor=white1;

//

//

// }

// else if(btn.tag==103){

// UILabel *data=(UILabel *)[self.view viewWithTag:203];

// UILabel *shuzi=(UILabel *)[self.view viewWithTag:303];

// data.backgroundColor=[UIColor blueColor];

// shuzi.backgroundColor=[UIColor blueColor];

// shuzi.textColor=white1;

// data.textColor=white1;

//

//

// }

// else{

// UILabel *data=(UILabel *)[self.view viewWithTag:204];

// UILabel *shuzi=(UILabel *)[self.view viewWithTag:304];

// data.backgroundColor=[UIColor blueColor];

// shuzi.backgroundColor=[UIColor blueColor];

// shuzi.textColor=white1;

// data.textColor=white1;

//

//

//

// }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: