1

我想在另一张图片上显示一张图片。基本上我试图显示该框已选中。问题是,当单击未选中的图像时,会出现复选按钮,但复选框会熄灭。我希望复选符号出现在复选框的顶部。

代码如下所示:

if (ShopCartUnedited) {
            [button setBackgroundImage:[UIImage imageNamed:@"none.png"] forState:UIControlStateNormal];
            [arrOfObjects addObject:@"NO"];
        }
        else {
            if (counter+1 <= nQty) {
                [button setBackgroundImage:[UIImage imageNamed:@"check2.png"] forState:UIControlStateNormal];
                [arrOfObjects addObject:@"YES"];
            }
            else {
                [button setBackgroundImage:[UIImage imageNamed:@"none.png"] forState:UIControlStateNormal];
                [arrOfObjects addObject:@"NO"];
            } 
        }

如果单击时,我如何更改 check2.png 位于 none.png 之上...

4

2 回答 2

1

您可以有两个不同的图像,一个带有复选标记,另一个没有复选标记,并在它们之间切换。如果这是不可能的,您需要在按钮顶部添加一个单独的图像视图check2.png

//create an imageview and add on button, adjust the frame as required.
[button addSubview:imageview];
imageview.userInteractionEnabled = NO;//if button is not accepting the touches
[button setBackgroundImage:[UIImage imageNamed:@"none.png"] forState:UIControlStateNormal];

        if (ShopCartUnedited) {
            [imageview setImage:nil];
            [arrOfObjects addObject:@"NO"];
        }
        else {
            if (counter+1 <= nQty) {
                [imageview setImage:[UIImage imageNamed:@"check2.png"]];
                [arrOfObjects addObject:@"YES"];
            }
            else {
                [imageview setImage:nil];
                [arrOfObjects addObject:@"NO"];
            } 
        }

或者代替这个 if-else 块,只需使用

        if ((ShopCartUnedited) || (counter+1 > nQty)) {
            [imageview setImage:nil];
            [arrOfObjects addObject:@"NO"];
        } else {
             [imageview setImage:[UIImage imageNamed:@"check2.png"]];
             [arrOfObjects addObject:@"YES"];              
        }
于 2012-11-09T03:56:59.217 回答
0

一个简单的方法是在选中状态和默认状态下使用框图像作为按钮的背景图像,同时将检查图像设置为选中状态的按钮上的图像。并管理选中或取消选中按钮的条件。

如下所示: btn.selected = !btn.selected;

谢谢

于 2012-11-09T06:51:06.623 回答