2014-03-31 25 views
1

我試圖改變UIButton圖像的顏色。在viewDidLoad方法中,我將着色顏色更改爲「appColor」或灰色,工作正常。當用戶點擊按鈕時,我嘗試再次更改色調顏色,但沒有任何反應。我甚至試圖用UIButton.imageView setImage來改變圖像,也沒有任何反應。我究竟做錯了什麼?爲什麼不UIButton.imageView setTintColor:工作在IBAction

這工作

- (void)viewDidLoad{ 
    if ([checkActivityArray containsObject:[place objectId]]){ 
     [checkCount setTextColor:appColor]; 
     checkButton.imageView.image = [checkButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
     [checkButton.imageView setTintColor:appColor]; 
    } 
    else{ 
     [checkCount setTextColor:[UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1]]; 
     checkButton.imageView.image = [checkButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
     [checkButton.imageView setTintColor:[UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1]]; 
    } 
} 

這不

- (IBAction)checkMarkButton:(UIButton *)sender { 
    sender.enabled = NO; 
    CheckMarkController *checkMark = [[CheckMarkController alloc]init]; 
    BOOL didComplete = NO; 

    if ([checkActivityArray containsObject:[place objectId]]){ 
     didComplete = [checkMark removeCheckMark:place]; 
    }else{ 
     didComplete = [checkMark addCheckMark:place]; 
    } 
    if (didComplete) { 

     checkActivityArray = [[NSMutableArray alloc] initWithContentsOfFile:checkMarkArrayFileName]; 

     int tempInt = [checkCount.text intValue]; 

     if ([checkActivityArray containsObject:[place objectId]]){ 
      [sender.imageView setImage:[checkButton.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]]; 
      [sender.imageView setTintColor:appColor]; 
      [checkCount setTextColor:appColor]; 
      tempInt++; 
      checkCount.text = [NSString stringWithFormat:@"%d", tempInt]; 
     } 
     else{ 
      sender.imageView.image = [sender.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; 
      [sender.imageView setTintColor:[UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1]]; 
      [checkCount setTextColor:[UIColor colorWithRed:170.0/255.0 green:170.0/255.0 blue:170.0/255.0 alpha:1]]; 
      tempInt--; 
      checkCount.text = [NSString stringWithFormat:@"%d", tempInt]; 
     } 
     sender.enabled = YES; 
    } 


} 

回答

0

按照iOS Developer Library

要刷新子視圖渲染時此屬性更改,覆蓋tintColorDidChange方法。

而且tintColorDidChange方法定義如下:

UIView tintColorDidChange

系統時,你的代碼更改該視圖的tintColor屬性的值調用視圖此方法。另外,系統在繼承改變的交互色調顏色的子視圖上調用該方法。

在您的實施中,根據需要刷新視圖渲染。

+0

我需要在tintColorDidChange中放置什麼? – david2391

+0

我試過[self setNeedsDisplay];仍然沒有什麼 – david2391

+0

調用tintColorDidChange方法嗎? – user1459524