如果未选中所有其他复选框,则始终选中如何设置1复选框

时间:2014-10-25 07:44:48

标签: ios xcode checkbox uibutton

如果未选中所有其他复选框,如何设置始终选中1复选框 我使用的下面的代码非常好,但我无法设置1个复选框

     int x=0;

    checkbox1 = [[UIButton alloc] initWithFrame:CGRectMake(350,600+x,50,50)];

    [checkbox1 setBackgroundImage:[UIImage imageNamed:@"checkboxunchecked.png"]forState:UIControlStateNormal];
    [checkbox1 setBackgroundImage:[UIImage imageNamed:@"checked.png"]forState:UIControlStateSelected];
    [checkbox1 setBackgroundImage:[UIImage imageNamed:@"checked.png"]forState:UIControlStateHighlighted];

    checkbox1.adjustsImageWhenHighlighted=NO;
    checkbox1.tag=1;
    [checkbox1 addTarget:self action:@selector(checkboxSelectedMethod:) forControlEvents:UIControlEventTouchUpInside ];
    [self.view addSubview:checkbox1];
    x+=checkbox1.frame.size.width*1.5;


    checkbox2 = [[UIButton alloc] initWithFrame:CGRectMake(350,600+x,50,50)];

    [checkbox2 setBackgroundImage:[UIImage imageNamed:@"checkboxunchecked.png"]forState:UIControlStateNormal];
    [checkbox2 setBackgroundImage:[UIImage imageNamed:@"checked.png"]forState:UIControlStateSelected];
    [checkbox2 setBackgroundImage:[UIImage imageNamed:@"checked.png"]forState:UIControlStateHighlighted];

    checkbox2.adjustsImageWhenHighlighted=NO;
    checkbox2.tag=1;
    [checkbox2 addTarget:self action:@selector(checkboxSelectedMethod:) forControlEvents:UIControlEventTouchUpInside ];
    [self.view addSubview:checkbox2];
    x+=checkbox2.frame.size.width*1.5;

类似地,还有2个按钮,总共有4个按钮,如果没有选择其他3个,则应该选择第一个按钮

-(void)checkboxSelectedMethod:(id)sender
{
UIButton *btn = (UIButton *) sender;
if([checkbox1 isSelected])
{
    [btn setSelected:NO];

}
else{

    [btn setSelected:YES];
}
if([checkbox2 isSelected])
{
    [btn setSelected:NO];
}
else{
    [btn setSelected:YES];
}

}

1 个答案:

答案 0 :(得分:0)

如果使用数组来存储按钮而不是离散变量,那将会容易得多 -

@property (strong,nonatomic) NSMutableArray *checkboxes;
@property (strong,nonatomic) UIImage *checkedImage;
@property (strong,nonatomic) UIImage *checkedImage;
@property (strong,nonatomic) NSMutableIndexSet *selectedSet;


int x=0;

self.uncheckedImage=[UIImage imageNamed:@"checkboxunchecked.png"];
self.checkedImage=[UIImage imageNamed:@"checked.png"];

self.selectedSet=[NSMutableIndexSet new];      //Indexset to store selected buttons

self.checkboxes=[NSMutableArray new];

for (int i=0i;i<3;i++) {
    UIButton *checkbox = [[UIButton alloc] initWithFrame:CGRectMake(350,600+x,50,50)];

    [checkbox setBackgroundImage:uncheckedImage forState:UIControlStateNormal];
    [checkbox setBackgroundImage:checkedImage forState:UIControlStateSelected];
    [checkbox setBackgroundImage:uncheckedImage forState:UIControlStateHighlighted];

    checkbox.adjustsImageWhenHighlighted=NO;
    checkbox.tag=i;
    [checkbox addTarget:self action:@selector(checkboxSelectedMethod:) forControlEvents:UIControlEventTouchUpInside ];
    [self.view addSubview:checkbox];
    [self.checkboxes addObject:checkbox];
    x+=checkbox.frame.size.width*1.5;
}

现在您可以检查sender是否在索引集(已选中)中,如果集合中的计数> 1(选择了多个框),则将其删除。如果不在集合中,则不会选中它,因此选择它并将其添加到集合

-(void)checkboxSelectedMethod:(id)sender
{
    UIButton *btn = (UIButton *) sender;

    NSInteger buttonIndex=btn.tag;

    if ([self.selectedSet containsIndex:buttonIndex]) {
       if (self.selectedSet.count > 1) {
          [self.selectedSet removeIndex:buttonIndex];
          [btn setSelected:NO];
       }
    } else {
       [btn setSelected:YES];
       [self.selectedSet addIndex:buttonIndex];
    }
}