在iPhone中一次选择所有复选框

时间:2013-02-05 06:17:32

标签: iphone ios

我有8个复选框button s(标题为A,B,C,D,E,F,G,H)和一个复选框button(标题为“myButton” )

当我点击“myButton”复选框时,我想选中所有8个复选框。如果取消选中8中的任何一个,则取消选中“myButton”复选框。

让我知道是否有人回答。

2 个答案:

答案 0 :(得分:0)

myButton复选框提供以下复选框标签:1,2,3,...,8和9。

点击myButton

if(//If my check box button is not already selected)
{
   //check all
   for (int loop = 1; loop<9;loop++)
   {
     UIButton *check = (UIButton *)[self.view viewWithTag:loop];
     //check that button
   }
}
else
{
   //uncheck all
}

要取消选中您可以使用的myButton

UIButton *check = (UIButton *)[self.view viewWithTag:9];
//uncheck it

编辑:

如果您创建了用于放置按钮的中间视图,请使用该视图而不是self.view

 UIButton *check = (UIButton *)[placedView viewWithTag:loop];

答案 1 :(得分:0)

 //Creating a button

    for (int i = 100; i < 108; i++) {

        UIButton *checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        checkBtn.tag = i;
        checkBtn.frame = CGRectMake(15, 10+30*i-100, 250, 30);
        checkBtn.titleLabel.text = [NSString stringWithFormat:@"Btn %d",i];
        [checkBtn setBackgroundImage:[UIImage imageNamed:@"uncheck.png"] forState:UIControlStateNormal];
        [customView addSubview:checkBtn];

    }

选择所有按钮

-(void) selectAll:(id)sender
{
    for (int i = 100; i < 108; i++) {

        UIButton *selectBtn = (UIButton*)[customView viewWithTag:i];
        [selectBtn setBackgroundImage:[UIImage imageNamed:@"check.png"] forState:UIControlStateNormal];

    }
}