2

如何实现自定义复选标记按钮取决于数组计数,并且需要在这些按钮中允许多选。目前我是这样使用的,

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];
         }
     }

但是,在选择时只选择最后一个索引按钮。

4

1 回答 1

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];



        }
    }
于 2013-08-10T13:01:49.943 回答