2012-08-24 18 views
0

所以我改變一個按鈕的圖像和一個UIImageView使用這兩個函數的圖像:的UIImage不更新上setImage按鈕IBAction爲

- (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:%@」,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變量。

感謝您的幫助和如此迅速!

+1

「current_badge_image」在哪裏定義? –

回答

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

試試這個,因爲我知道你需要設置該按鈕的狀態。這段代碼將設置圖像及其不是和background.let我知道是否有任何其他的事情。

+0

我現在更新了我的問題和答案。實際上是2或3件事導致它不能正常工作。所有在那裏解釋。感謝您的幫助! – jimbob

1

要設置UIButton的圖像,您必須指定要使用它的圖像UIControlStateimageView屬性不是用於設置背景圖像,而是用於configure the appearance and behavior of the button’s view

設置圖像的正確方法是

[changeButtonOutlet setImage:imageToSet forState:UIControlStateNormal]; 

的控制狀態和他們的應用程序中列出here

+0

好吧那麼UIImageView呢?這一個不是一個按鈕。 – jimbob

+1

你確定你的'current_badge_image'和'current_group_image'對象是有效的嗎? – Alexander

+0

是current_badge_image和current_group_image是完全填充的。 – jimbob