2013-07-22 91 views
0

我定義了一個自定義的initWithCoder方法,因爲我試圖製作自定義工具欄項目。我的故事板中有5個視圖控制器,每個都有一個自定義類。它們都是初始視圖控制器類的子類。主要的問題是,不是隻改變一個類別標籤欄項目的圖像,而是改變每個被按下的標籤欄項目的圖像。此外,當標籤欄項目未被選擇時,圖像不會改變爲未選擇的標籤欄項目圖像。每個子類視圖控制器都接收相同的init方法。我的主要問題是如何讓每個類,不管孩子或父母,接受不同的init方法?InitWithCoder多次調用

-(id)initWithCoder:(NSCoder *)aDecoder 

{ 
     NSLog(@"First"); 
     if (self = [super initWithCoder:aDecoder]) 

    { 
      NSLog(@"Second"); 
      [[self tabBarItem] setFinishedSelectedImage:[UIImage imageNamed:@"RedStar2.png"] 
      withFinishedUnselectedImage:[UIImage imageNamed:@"Star3.png"]]; 
      //instead of just changing the image of the class's tab bar item, it changes the image of all 
      classes tab bar items 
      [[self tabBarItem] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor 
      redColor], UITextAttributeTextColor, nil] forState:UIControlStateNormal]; 
     } 
     return self; 
} 

當應用程序運行時,關鍵字「first」和「second」都被調用5次。每個視圖控制器都有一個自定義類。

+6

你有5個視圖控制器,這個的子類...'initWithCoder'被調用5次...«嘿,你期望什麼? » – Cyrille

+0

我應該使用其他什麼方法? – user1626438

+0

如果你顯示5個viewControllers,可以顯示5倍這些消息是可以的......這不是'我應該使用什麼其他方法?' – Peres

回答

0

如果你想爲每個標籤選擇一個不同的圖像,你必須在每個子類中重複5次這個方法,而不是在「共同祖先」 - 超類中做同樣的工作。