>我有一个按钮,应该代表一个复选框。单击该按钮时,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 语句,控制台显示了这一点
当前 <89504E47 0D0A1A0A 0000000D 49484452 00000019 00000013 08060000 006572A6 05000000 01735247 4200AECE 1CE90000 001C6944 4F540000 00020000 0000000 000A0000 00280000 000A0000 00090000 01066167 14D70000 00D24944 4154480D B48D3D0A C2401046 7304C142 116F6021 587a121b 1b1b151b 1b5b6f21 5a780183 781425e0 45442b09 ab6f703799950125 9081d9f9 7bdf7e49 526338e7 66e4be36 0b3e9fbe 08aaabc5 847fe762 2041BF88 4C589C3E A76A2FFA 1D390C6A FA756420 03CB6300 AA54F459 D0C95F85 01C3400E D43325F5 FDB800FE68d0ae44 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 4F4531366 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 93d5377a5DEBA59B 98C0AA74 791BDCD 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 0000000 000A0000 00280000 000A0000 00090000 01130CBA F03C0000 00DF4944 4154480D B4913B0A C2401086 7304C142 116F602 1 587a121b 1b1b151b 1b5b6f21 5a780183 781425e0 45442b71 b37ea3fb 84051f90 81ecccce 7cfffc24 c9b20a43 6b3d514a 6d2bb3c0 60cca34b a2121376 4fc5c0c4 2c32c1f46027ff64 3ecd065d 3fd02e23 03b960b2 0f809f4b f48515c9 2e6740b3 27039a47 522e353174C01705 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 D349 4441 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 D665064492C942A2 DCEC2831 817CA71E C257D5F0 0B1AAAA07 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 0000000 00090000 00280000 00090000 000900000065C6D0 72350000 00314944 41543811 62601805 A484C07F 0A0141BB 2834FFF 3F D1161054 88A600E6 3034614C 2ed10ad1 b412ad8f 688583c9 02000000 00ffff66 fa95da00 00003949 44415463 604002ff a1004988 2826d1fa 88568866 2dd1fa88 56386a01 5a0810e4 121db444 2b44b392 687d442b a4d40298 45a4d268 f682b900 f6d17e903cc7b79b 00000000 49454e44 ae426082>
但是我没有对中间执行的代码中的图像做任何事情。(在这种情况下(当前图像和检查图像不同的原因可能是什么?
谢谢!
使用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
将在UIButton
上NO
并unchecked
image
。
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;