UIImage没有在按钮IBAction上的setImage上更新

时间:2012-08-24 11:22:37

标签: 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 ]];    
    }
    }

}

然而图像不更新。如果我垃圾邮件改变了改变按钮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

我做错了什么?谢谢你们!


编辑:固定它!这是更新的代码:

- (无效)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变量。

感谢所有帮助,所以很快!

2 个答案:

答案 0 :(得分:3)

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

试试这个,因为我知道您需要为该按钮设置状态。这段代码将设置Image及其not和background.let我知道是否还有其他东西。

答案 1 :(得分:1)

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

设置图像的正确方法是

[changeButtonOutlet setImage:imageToSet forState:UIControlStateNormal];

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