Search code examples
iosipaduibuttoncheckmark

How to implement checkmark buttons in iPad out side of tableview


How to implement custom checkmark buttons depends on array count, and need to allow multiple selection in those buttons. Currently I'm used like this,

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

Setting images for check mark:

    [checkmarkButton setBackgroundImage:[UIImage imageNamed:@"checkBox_none"] forState:UIControlStateNormal];
    [checkmarkButton addTarget:self action:@selector(serviceSelected:) forControlEvents:UIControlEventTouchUpInside];

then I had implement the action method.

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

But, at the time of selecting only last index button is selected.


Solution

  • try this code:

    check your image extension png or 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];
    
    
    
            }
        }