我有一个代表复选框的按钮。单击该按钮时,imageView.image-Property将设置为另一个UIImage。所以,我有两个变量用于选中和未选中的图像。
UIImage *checkedImage = [UIImage imageNamed:@"check_box_checked_white.png"];
UIImage *uncheckedImage= [UIImage imageNamed:@"check_box_unchecked_white.png"];
点击按钮,执行以下方法
- (void)checkButton:(UIButton *)sender {
NSLog(@"current %@", UIImagePNGRepresentation(sender.imageView.image));
NSLog(@"checked %@", UIImagePNGRepresentation(checkedImage));
NSLog(@"unchecked %@", UIImagePNGRepresentation(uncheckedImage));
if ([UIImagePNGRepresentation(sender.imageView.image) isEqualToData:UIImagePNGRepresentation(checkedImage)]){
[sender setImage: [UIImage imageNamed:@"check_box_unchecked_white.png"] forState: UIControlStateNormal];
}
else if ([UIImagePNGRepresentation(sender.imageView.image) isEqualToData:UIImagePNGRepresentation(uncheckedImage)]){
[sender setImage: [UIImage imageNamed:@"check_box_checked_white.png"] forState: UIControlStateNormal];
} }
两个if子句的声明都没有,对于NSLog-Statements,控制台显示这个
电流I; 89504e47 0d0a1a0a 0000000D 49484452 00000019 00000013 08060000 006572a6 05000000 01735247 4200aece 1ce90000 001c6944 4f540000 00020000 00000000 000a0000 00280000 000a0000 00090000 01066167 14d70000 00d24944 4154480d b48d3d0a c2401046 7304c142 116f6021 587a121b 1b1b151b 1b5b6f21 5a780183 781425e0 45442b09 ab6f7037 99950125 9081d9f9 7bdf7e49 526338e7 66e4be36 0b3e9fbe 08aaabc5 847fe762 2041bf88 4c589c3e a76a2ffa 1d390c6a fa756420 03cb6300 aa54f459 d0c95f85 01c3400e d43325f5 fdb800fe 68d0ae44 a7228d64 004dd209 40c97d9d 44d08f01 dd3218d0 5f4d9c43 9f7c2a70 6482c612 5d57e92e 0652ae00 db086e22 a0e664ab bcda1d4c 877c2893 8d4daa2d 82431078 b39e3a47 2df706fc fd8bdf46 901fde00 0000FFFF f01a55c5 000000cf 49444154 ad92bd09 02411484 4f453136 b014c1cc 5030b403 41033b10 0b10ccc5 16446cc0 d02a844b 4d8d4e53 57bf39df c2b226e7 eac0e3cd fb9b81db cbb200ce b9c31390 0b65013e 0a564a4a bb4d3fb7 f905be30 be8d773f 6a96bdc9 187ed4a1 009ffb65 ca16f5d9 fa05bc43 24994c24 caf14e62 027c6dbd 93d5377a 5deb a59b 98c0aa74 791bddcd e081414f 7301fe9b 8944109e 054668ba befa1e7f 31911842 43e28ad9 c08bfb9c 6a3245ac 4134a3a8 45b5e675 4c9664bd 5bf5bf4b 0729a864 82f09e45 410ffb6d e86ee33f 61985f0d b75066ce 264e2e00 00000049 454e44ae 426082>
检查< 89504e47 0d0a1a0a 0000000D 49484452 00000019 00000013 08060000 006572a6 05000000 01735247 4200aece 1ce90000 001c6944 4f540000 00020000 00000000 000a0000 00280000 000a0000 00090000 01130cba f03c0000 00df4944 4154480d b4913b0a c2401086 7304c142 116f6021 587a121b 1b1b151b 1b5b6f21 5a780183 781425e0 45442b71 b37ea3fb 84051f90 81ecccce 7cfffc24 c9b20a43 6b3d514a 6d2bb3c0 60cca34b a2121376 4fc5c0c4 2c32c1f4 6027ff64 3ecd065d 3fd02e23 03b960b2 0f809f4b f48515c9 2e6740b3 27039a47 522e3531 74c01705 fce2a5f2 471ec9e8 d7312865 4e7a186e 14411f2e 68e64627 3bce491c a0cbf06e 41f22009 269ab06d ab63c729 81f81660 13e82202 b2bc51c3 4fd3154c 0bf6261a 097efc2a 4d065da0 dd1b7767 27184725 440d83ab 2329d0af 23c85c9e 000000ff ffc36350 d7000000 d3494441 54a5923d 0ac24014 84a3a258 5b7814c1 ce52b0f4 06422cbc 817800c1 5ebc8288 17b0f414 82adad55 b4ddd56f c20b2c6b 6336032f f3fe7666 59926501 bcf7e70f 800bb161 16ac9429 fd3e3b77 cde107b4 56ee9c3b c4bb3f75 65c2fe9c fca28386 55b54cdd 6376531f d6650644 92c9 42a2 dcec2831 817ca71e c257d5f0 0b1aaa07 a79b4800 f1ad4405 84dfc60e 1e692e90 37333191 a5c4058c 3c342ed5 ed43dddc c48ca6e8 3f119c84 06364b32 c911eb10 dd285a51 ad799bd8 10b5ff2e 9da98dbf 7e61544f 7a73e012 c263b28f 9f52f517 246950a5 7cf4e6af 00000000 49454e44 ae426082>
选中< 89504e47 0d0a1a0a 0000000D 49484452 00000018 00000012 08060000 0041ec1e 9e000000 01735247 4200aece 1ce90000 001c6944 4f540000 00020000 00000000 00090000 00280000 00090000 00090000 0065c6d0 72350000 00314944 41543811 62601805 a484c07f 0a0141bb 2834ff3f d1161054 88a600e6 3034614c 2ed10ad1 b412ad8f 688583c9 02000000 00ffff66 fa95da00 00003949 44415463 604002ff a1004988 2826d1fa 88568866 2dd1fa88 56386a01 5a0810e4 121db444 2b44b392 687d442b a4d40298 45a4d268 f682b900 f6d17e90 3cc7b79b 00000000 49454e44 ae426082>
但是我没有对中间执行的代码中的图像做任何事情。 (在这种情况下)当前图像和检查图像不一样的原因是什么?
谢谢!
答案 0 :(得分:1)
使用UIButton's
currentImage
属性和isEqual
方法进行图像比较,如下所示:
UIButton *btnClicked = (UIButton *)sender;
UIImage *checkedImage = [UIImage imageNamed:@"check_box_checked_white.png"];
UIImage *uncheckedImage= [UIImage imageNamed:@"check_box_unchecked_white.png"];
if([btnClicked.currentImage isEqual:checkedImage]) //Check if it has checked image or not
{
//so set unchecked image
[btnClicked setImage:uncheckedImage forState: UIControlStateNormal];
}
else //button has unchecked image
{
//so set checked image
[btnClicked setImage:checkedImage forState: UIControlStateNormal];
}
编辑:正如@Michael Olenick在评论中所建议的那样,最好使用UIButton's
selected
属性。在state
上默认NO
为unchecked
和image
UIButton
。
if(btnClicked.selected) //Check if it has checked image or not
{
//so set unchecked image
[btnClicked setImage:uncheckedImage forState: UIControlStateNormal];
}
else //button has unchecked image
{
//so set checked image
[btnClicked setImage:checkedImage forState: UIControlStateNormal];
}
btnClicked.selected = !btnClicked.selected;