-1

我做了一個自定義UITabBar工作得很好,但我介紹了導航的變化,現在我有一些嚴重的問題。以下是我有:自定義TabBar失去TabBarItem圖像

常規設置

TabBarController 
    NavbarController1 - TabBarItem1 
     Links to: PeopleView 
    NavbarController2 - TabBarItem2 
     Links to: ContentView 
    NavbarController3 - TabBarItem3 
     Links to: ContentView //Same VC as TabBaritem 2. 

應用代表 - 在我的didFinishLaunchingWithOptions方法我稱之爲customizeTabBar方法如下

-(void) customizeTabBar 
{ 

UITabBarController *tabVC = (UITabBarController *) self.window.rootViewController; 
//Load Navigation Bar images 
NSArray *unSelectedImages = [[NSArray alloc] initWithObjects:@"image1.jpg", @"image2.jpg", @"image3.jpg", @"image4.jpg", @"image5.jpg", nil]; 
NSArray *selectedImages = [[NSArray alloc] initWithObjects:@"image1_on.jpg", @"image2_on.jpg", @"image3_on.jpg", @"image4_on.jpg", @"image5_on.jpg", nil]; 

NSArray *items = tabVC.tabBar.items; 

for (int idx = 0; idx < items.count; idx++) 
{ 

    UITabBarItem *barItem = [items objectAtIndex:idx]; 
    barItem.tag = idx; 
    UIImage *selectedImage = [UIImage imageNamed:[selectedImages objectAtIndex:idx]]; 
    UIImage *unSelectedImage = [UIImage imageNamed:[unSelectedImages objectAtIndex:idx]]; 


    UIEdgeInsets inset = { 
     .top = 10, 
     .left = 0, 
     .bottom = -10, 
     .right = 0 
    }; 

    barItem.imageInsets = inset; 

    [barItem setFinishedSelectedImage:selectedImage withFinishedUnselectedImage:unSelectedImage]; 
} 

到目前爲止,這是工作精美。 。

這裏是哪裏出了問題進來。爲了讓我TabBarItem3鏈接到內容查看, 我實現下面的代碼在我TabBarClass:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController { 

if (viewController.tabBarItem.tag == 1 || viewController.tabBarItem.tag == 2) 
{ 
    // Validating if is necesarry to replace the TabBarController.ViewControllers 
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; 

    MediaList *mediaView = [storyboard instantiateViewControllerWithIdentifier:@"SB_MediaList"]; 
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:mediaView]; 
    NSMutableArray *viewsArray = [NSMutableArray arrayWithArray:self.viewControllers]; 


    if (viewController.tabBarItem.tag == 1) 
    { 
     //Setting the specfic data for my instance for tabBarItem 1 
     NSLog(@"Here we are in 1"); 
     [mediaView setContent:@"Personalized content/data for TabBarItem 1"]; 
    } 
    else if (viewController.tabBarItem.tag == 2) 
    { 
     //Setting the specfic data for my instance for tabBarItem 2 
     NSLog(@"Here we are in 2"); 
     [mediaView setContent:@"Personalized content/data for TabBarItem 2"]; 
    } 
    [viewsArray replaceObjectAtIndex:viewController.tabBarItem.tag withObject:navigationController]; 
    self.viewControllers = viewsArray; 

} 
} 

在此代碼的執行,我失去了圖片與項目2或3的自定義選項卡相關聯(取決於我選擇的項目)。

UPDATE

所以,我提出我的標籤欄的定製方法了我的委託,併成爲一個maintabbar類,我把它在viewDidLoad中和我的MEH HID didselectviewcontroller。這似乎解決了丟失圖像的問題,但是當單擊任一軟管項目時,在屏幕上產生了閃爍的不良副作用。我已經嘗試了不同的組合,從viewdidload方法中刪除它,因爲我們會,但仍然沒有運氣..

回答

0

據我瞭解,這整個事情的目的是爲了避免必須創建兩個標籤欄項目的不同類,將執行幾乎相同的事情,但具有不同的數據。

所以,你必須定義一個能夠做這種事情的類,我們稱之爲CommonClass。這個類將不得不有必要的方法來進行正確的初始化。 [我相信你已經做到了這一點]。

然後在你的故事板上,你仍然需要有兩個ViewController元素,每個元素作爲rootViewController對應的NavigationController

enter image description here

我強烈建議這樣做的方式,而不是試圖通過代碼,我們試圖在分配一個新的實例:Linking to a view multiple times from tabar。這兩個視圖控制器顯然屬於同一類CommonClass,所以您仍然會重用該類。通過這種方式,我們不必在執行時間內更改NavigationBar,這可能很麻煩。

然後,您可以發送必要的初始化信息爲您CommonClass爲您CommonClass的方法,將根據所選擇的tabBarItem加載此時,相應的信息/數據,你可以做到這一點的方法viewDidLoad

如下所示:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    if (self.tabBarController.selectedIndex == 1) { 
     [self customizeWithData:@"Specific data for my first instance"]; 
    } else if (self.tabBarController.selectedIndex == 2) { 
     [self customizeWithData:@"Specific data for my second instance"];   
    } 
} 

這樣你會做在短短的一個代碼的一部分,你的標籤欄的定製和你會不會改變它在執行時間你贏了」 t需要在其他部分定製導致閃爍。

+0

我已經採取了你的指導和感動一切。我更喜歡設計,但不幸的是閃爍問題依然存在。請參閱http://stackoverflow.com/q/19123984/2804909 – mday