如何实现自定义复选标记按钮取决于数组计数,并且需要允许在这些按钮中进行多项选择。目前我这样使用,
int YAxis = 300;
for (int x=0; x< [myResultArray count];x++)
{
checkmarkButton = [UIButton buttonWithType:UIButtonTypeCustom];
checkmarkButton.frame = CGRectMake(100, YAxis, 20, 20);
checkmarkButton.tag = selectedServiceTag;
[self.view addSubview:checkmarkButton];YAxis = YAxis+30;
}
设置复选标记的图像:
[checkmarkButton setBackgroundImage:[UIImage imageNamed:@"checkBox_none"] forState:UIControlStateNormal];
[checkmarkButton addTarget:self action:@selector(serviceSelected:) forControlEvents:UIControlEventTouchUpInside];
然后我实现了动作方法。
-(void)serviceSelected
{
checkmarkButton.selected = !checkmarkButton.selected;
if (checkmarkButton.selected)
{
[checkmarkButton setBackgroundImage:[UIImage imageNamed:@"checkBox"] forState:UIControlStateNormal];
}
else
{
[checkmarkButton setBackgroundImage:[UIImage imageNamed:@"checkBox_none"] forState:UIControlStateNormal];
}
}
但是,在选择时只选择了最后一个索引按钮。
答案 0 :(得分:0)
试试这段代码:
检查您的图片扩展程序png或jpg
-(IBAction)serviceSelected:(id)sender
{
UIButton *tmpYes = (UIButton *)sender;
if (tmpYes.currentBackgroundImage==[UIImage imageNamed:@"checkBox.png"]) {
[tmpYes setBackgroundImage:[UIImage imageNamed:@"checkBox_none.png"] forState:UIControlStateNormal];
}
else
{
[tmpYes setBackgroundImage:[UIImage imageNamed:@"checkBox_none.png"] forState:UIControlStateNormal];
}
}