ios - UIImage 未在按钮 IBAction 上的 setImage 上更新

标签 ios xcode uiimageview uibutton

所以我正在使用这两个函数更改按钮的图像和 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(@"当前角标(Badge)图片: %@", 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 变量。

感谢所有的帮助,而且这么快!

最佳答案

UIImage *image = [UIImage imageNamed:@"image.png"];
[changeButtonOutlet setImage:image forState:UIControlStateNormal];

试试这个,据我所知,您需要为该按钮设置状态。此代码将设置图像及其非图像和背景。如果有任何其他内容,请告诉我。

关于ios - UIImage 未在按钮 IBAction 上的 setImage 上更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12108449/

相关文章:

ios - React Native - iOS 模拟器部署目标 'IPHONEOS_DEPLOYMENT_TARGET' 设置为 7.0

ios - SWRevealViewController开启方法

ios - iOS 8 中的相机显示黑屏

iphone - 我必须提供哪些选项才能提供允许对 UITableView 进行排序的功能?

ios - XCode 给我以下 "Apple Mach-O Linker"警告

ios - 为什么我的 UIImageView 替换了第二个?

ios - 如何将图像裁剪成圆角

ios - 如何检测 iOS 中的滑动手势?

xcode - 如何修复 xcode 错误 "web filter is active"?

ios - 如何在 Swift 中加载 GIF 图像?