所以我正在使用这两个函数更改按钮的图像和 UIImageView 的图像:
- (IBAction)changeButton:(id)sender {
//switch image to group if it is a badge. switch image to badge if it is a group.
if([[properties objectForKey:@"current_item"] isEqualToString:@"badge"]){
NSLog(@"reaching badge");
UIImage *imageToSet = [UIImage imageNamed:current_group_image];
[changeButtonOutlet.imageView setImage:imageToSet];
[properties setObject:@"group" forKey:@"current_item"];
} else {
if([[properties objectForKey:@"current_item"] isEqualToString:@"group"]){
NSLog(@"reaching group");
UIImage *imageToSet = [UIImage imageNamed:current_badge_image];
[changeButtonOutlet.imageView setImage:imageToSet];
[properties setObject:@"badge" forKey:@"current_item"];
}
}
[self switchBadgeImage];
}
-(void)switchBadgeImage{
if([[properties objectForKey:@"current_item"] isEqualToString:@"badge"]){
[badgeImageView setImage:[UIImage imageNamed:current_badge_image ]];
} else {
if([[properties objectForKey:@"current_item"] isEqualToString:@"group"]){
[badgeImageView setImage:[UIImage imageNamed:current_group_image ]];
}
}
}
但是图像不更新。如果我向 changeButton IBAction 发送垃圾邮件,有时我可以很快看到图像发生变化,但它总是会弹回默认图像(仅用于按钮)。
这是我反复按下按钮时控制台日志告诉我的内容:
2012-08-24 12:20:52.414 MyApp[1448:707] reaching group
2012-08-24 12:20:52.559 MyApp[1448:707] reaching badge
2012-08-24 12:20:52.687 MyApp[1448:707] reaching group
2012-08-24 12:20:52.846 MyApp[1448:707] reaching badge
2012-08-24 12:20:52.976 MyApp[1448:707] reaching group
2012-08-24 12:20:53.117 MyApp[1448:707] reaching badge
2012-08-24 12:20:53.262 MyApp[1448:707] reaching group
2012-08-24 12:20:53.348 MyApp[1448:707] reaching badge
我究竟做错了什么?多谢你们!
编辑:修复它!这是更新的代码:
-(void)switchBadgeImage{ NSLog(@"当前组图像:%@", current_group_image); NSLog(@"当前徽章图片:%@", current_badge_image);
current_item = [properties objectForKey:@"current_item"];
if([current_item isEqualToString:@"badge"]){
NSLog(@"current item is equal to badge");
[badgeImage setImage:[UIImage imageNamed:current_badge_image ]];
} else {
if([current_item isEqualToString:@"group"]){
NSLog(@"current item is equal to group");
[badgeImage setImage:[UIImage imageNamed:current_group_image ]];
}
}
}
- (IBAction)changeButton:(id)sender {
//switch image to group if it is a badge. switch image to badge if it is a group.
if([[properties objectForKey:@"current_item"] isEqualToString:@"badge"]){
NSLog(@"reaching badge");
UIImage *imageToSet = [UIImage imageNamed:current_badge_image];
[changeButtonOutlet setImage:imageToSet forState:UIControlStateNormal];
[properties setObject:@"group" forKey:@"current_item"];
[properties synchronize];
} else {
if([[properties objectForKey:@"current_item"] isEqualToString:@"group"]){
NSLog(@"reaching group");
UIImage *imageToSet = [UIImage imageNamed:current_group_image];
[changeButtonOutlet setImage:imageToSet forState:UIControlStateNormal];
[properties setObject:@"badge" forKey:@"current_item"];
[properties synchronize];
}
}
[self switchBadgeImage];
}
实际上有一些问题。首先,我以错误的方式切换图像。其次,我没有同步 UserDefaults。将当前项目保存到 UserDefaults 后,我也没有更新 current_item 变量。
感谢所有的帮助,如此之快!