【问题标题】:UIImage not updating on setImage on button IBActionUIImage 未在按钮 IBAction 上的 setImage 上更新
【发布时间】:2012-08-24 11:22:37
【问题描述】:

所以我正在使用这两个函数更改按钮的图像和 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 变量。

感谢大家的帮助,而且这么快!

【问题讨论】:

  • “current_badge_image”在哪里定义?

标签: ios xcode uiimageview uibutton


【解决方案1】:
UIImage *image = [UIImage imageNamed:@"image.png"];
[changeButtonOutlet setImage:image forState:UIControlStateNormal];

试试这个,我知道你需要为那个按钮设置状态。此代码将设置图像及其不和背景。如果还有其他东西,请告诉我。

【讨论】:

  • 我现在用答案更新了我的问题。实际上是 2 或 3 件事导致它无法正常工作。一切都在那里解释。谢谢你的帮助!
【解决方案2】:

要为UIButton 设置图像,您必须指定要使用它的UIControlStateimageView 属性不是用于设置背景图像,而是用于设置configure the appearance and behavior of the button’s view

设置图片的正确方法是

[changeButtonOutlet setImage:imageToSet forState:UIControlStateNormal];

列出了不同的控制状态及其应用here

【讨论】:

  • 好的,那么 UIImageView 呢?这个不是按钮所以。
  • 您确定您的current_badge_imagecurrent_group_image 对象有效吗?
  • 是的,current_badge_image 和 current_group_image 已完全填充。
  • 你为什么事件打电话changeButton:UIControlEventTouchUpInside 还是别的什么?
  • UIControlEventTouchUpInside 但是我现在已经修复了它......用修复更新我的帖子并提高你的答案:) 谢谢大家:3。真的很感激。
猜你喜欢
  • 2016-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多