对于我的测验应用程序,我通过设置活动和非活动图像为 4 个答案创建 4 个自定义单选按钮。当我选择按钮时,状态变为活动状态,但当我按下上一个按钮时,状态变为非活动状态。
创建自定义按钮的代码
//answer 1 targetLabel = [[UILabel alloc] init]; CGSize labelSize = CGSizeMake(240, 9999); CGSize theStringSize = [ans1 sizeWithFont:targetLabel.font constrainedToSize:labelSize lineBreakMode:targetLabel.lineBreakMode]; targetLabel.frame = CGRectMake(targetLabel0.frame.origin.x+40, targetLabel0.frame.size.height+40, theStringSize.width, theStringSize.height); targetLabel.text = ans1; [targetLabel setNumberOfLines:0]; targetLabel.backgroundColor=[UIColor clearColor]; [targetLabel sizeToFit]; [scroll addSubview:targetLabel]; // button 1 ans1btn = [[UIButton alloc] initWithFrame:CGRectMake(15,targetLabel.frame.origin.y-5,30,30)]; ans1btn.backgroundColor=[UIColor redColor]; [ans1btn setSelected:NO]; [ans1btn setBackgroundImage:[UIImage imageNamed:@"gray.png"] forState:UIControlStateNormal]; [ans1btn setBackgroundImage:[UIImage imageNamed:@"green.png"] forState:UIControlStateSelected]; ans1btn.adjustsImageWhenHighlighted=YES; [ans1btn addTarget:self action:@selector(checkboxSelected:) forControlEvents:UIControlEventTouchUpInside]; ans1btn.tag=1; [scroll addSubview:ans1btn];
调用方法
-(void)checkboxSelected:(id)sender
{
checkBoxSelected = TRUE;
[ans1btn setSelected:checkBoxSelected];
if(checkBoxSelected==TRUE)
{
checkBoxSelected1=FALSE;
[ans1btn1 setSelected:checkBoxSelected1];
[ans1btn2 setSelected:checkBoxSelected1];
[ans1btn3 setSelected:checkBoxSelected1];
checkBoxSelected=TRUE;
targetLabel.textColor=[UIColor blackColor];
targetLabel1.textColor=[UIColor grayColor];
targetLabel2.textColor=[UIColor grayColor];
targetLabel3.textColor=[UIColor grayColor];
}
else
{
NSLog(@"the button is not selected");
}
}
像这样我也在调用其他按钮动作
谁能帮帮我吗。
提前致谢。