复选框与图像有关

时间:2015-12-18 08:25:42

标签: ios

当第二次选中复选框时,它再次显示我的图像。请帮助。

- (IBAction)CheckBox:(id)sender {
    if ((CheckBox.selected==NO)
    {
        [CheckBox setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateNormal];
        CheckBox.selected = YES;
    }
    else
    {
       [CheckBox setBackgroundImage:[UIImage imageNamed:@"uncheck_checkbox.png"] forState:UIControlStateSelected];
       CheckBox.selected =NO;
    }
}

3 个答案:

答案 0 :(得分:1)

您需要编辑代码:

- (IBAction)CheckBox:(id)sender 
{
   [CheckBox setBackgroundImage:[UIImage imageNamed:@"uncheck_checkbox.png"] forState:UIControlStateSelected];
    [CheckBox setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateNormal];
    if ((CheckBox.selected==NO)
    {
         CheckBox.selected = YES;
    }
    else
    {
         CheckBox.selected =NO;
    } 
}

最好设置:

[CheckBox setBackgroundImage:[UIImage imageNamed:@"uncheck_checkbox.png"] forState:UIControlStateSelected];
[CheckBox setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateNormal];

在功能配置或您的viewDidLoad或您的xib中......

答案 1 :(得分:0)

你必须设置状态正常和选择

- (IBAction)CheckBox:(id)sender {
    if ((CheckBox.selected==NO)
    {
        [CheckBox setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateNormal];
        [CheckBox setBackgroundImage:[UIImage imageNamed:@"uncheck_checkbox.png"] forState:UIControlStateSelected];
    }
    else
    {
       [CheckBox setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateSelected];
       [CheckBox setBackgroundImage:[UIImage imageNamed:@"uncheck_checkbox.png"] forState:UIControlStateNormal];
    }
}

答案 2 :(得分:0)

- (IBAction)CheckBox:(id)sender {
    if ((sender.selected)
    {
        [sender setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateNormal];
        [sender setBackgroundImage:[UIImage imageNamed:@"uncheck_checkbox.png"] forState:UIControlStateSelected];
    }
    else
    {
       [sender setBackgroundImage:[UIImage imageNamed:@"checked_checkbox.png"] forState:UIControlStateSelected];
       [sender setBackgroundImage:[UIImage imageNamed:@"uncheck_checkbox.png"] forState:UIControlStateNormal];
    }
}