2013-03-20 127 views
0

我遇到了一個非常奇怪的問題。IOS在tabbarcontroller中從navigationcontroller推送viewcontroller,但不顯示

這裏是我的應用程序代理的代碼結構:

self.accountViewController = [[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil]; 
self.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; 
self.exploreViewController = [[ExploreViewController alloc] initWithNibName:@"ExploreViewController" bundle:nil]; 
self.activityViewController = [[ActivityViewController alloc] initWithNibName:@"ActivityViewController" bundle:nil]; 
self.homeNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController]; 
self.accountNavigationController = [[UINavigationController alloc] initWithRootViewController:self.accountViewController]; 
self.activityNavigationController = [[UINavigationController alloc] initWithRootViewController:self.activityViewController]; 
self.exploreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.exploreViewController]; 

self.tabBarController = [[MyOwnTabBarController alloc] init]; 
[self.tabBarController setDelegate:self]; 
[self.tabBarController setViewControllers:[NSArray arrayWithObjects:self.homeNavigationController, self.exploreNavigationController,self.activityNavigationController,self.accountNavigationController,nil]]; 
[self.tabBarController setSelectedIndex:0]; 
[self.navController setViewControllers:[NSArray arrayWithObjects:self.welcomeViewController, self.tabBarController, nil] animated:NO]; 

爲self.navController,我定義了tabbarcontroller提出這樣

self.navController = [[UINavigationController alloc] initWithRootViewController:self.welcomeViewController]; 
self.window.rootViewController = self.navController; 

所以,我將在homeviewcontroller,我有一個collectionview。它成功顯示多個單元格。當我點擊該單元格,

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.navigationController pushViewController:vc animated:YES]; 
} 

我看到導航欄與新標題改變了「VC」,並在導航欄的後退按鈕也顯示出來。但是,視圖沒有改變,我仍然可以訪問collectionview。但是,如果我在tabbarcontroller上的其他選項卡(如accountviewcontroller)上按下,然後再次按回到homeviewcontroller的舊選項卡,則視圖現在將顯示出來。這是一個非常奇怪的行爲,我不知道爲什麼會發生。

我還檢查,在每個視圖控制器,所述self.navigationcontroller不是nil

+0

你需要更徹底地描述你的應用程序結構。你是否在故事板中製作控制器?在代碼中? – rdelmar 2013-03-20 05:53:27

+0

有一個非常類似的問題,一個星期前從來沒有得到答案。沒有完全描述你的設置,你不會得到任何有用的幫助。 – rdelmar 2013-03-20 06:20:12

+0

好的,用代碼結構更新 – 2013-03-20 06:29:15

回答

0

我的理解。只要檢查你是否已經將navigationController添加到tabBar。在下面檢查。如果不是傳達我的總場景。

tabBarControllerObj=[[UITabBarController alloc]init]; 
    NSArray *arrayObj=[[NSArray alloc]initWithObjects:navForView1,navForView2,navForView3,navForView4,navForView5, nil]; 

    [tabBarControllerObj setSelectedIndex:0]; 
    tabBarControllerObj.tabBar.hidden = YES; 
    [tabBarControllerObj setViewControllers:arrayObj]; 
    [self.window addSubview:tabBarControllerObj.view]; 
+0

一樣使用其他viewcontroller是的,我確定navigationcontroller被添加到tabbar。訪問self.navigationcontroller不是無 – 2013-03-20 06:00:12

+0

是的,但您必須將navigationController添加到tabBar。你有沒有添加它們。 – Rahul 2013-03-20 06:07:38

+0

是的,我添加了它。如果我不添加它,我甚至無法看到視圖。 – 2013-03-20 06:21:25

0
self.accountViewController = [[AccountViewController alloc] initWithNibName:@"AccountViewController" bundle:nil]; 
self.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil]; 
self.exploreViewController = [[ExploreViewController alloc] initWithNibName:@"ExploreViewController" bundle:nil]; 
self.activityViewController = [[ActivityViewController alloc] initWithNibName:@"ActivityViewController" bundle:nil]; 
self.homeNavigationController = [[UINavigationController alloc] initWithRootViewController:self.homeViewController]; 
self.accountNavigationController = [[UINavigationController alloc] initWithRootViewController:self.accountViewController]; 
self.activityNavigationController = [[UINavigationController alloc] initWithRootViewController:self.activityViewController]; 
self.exploreNavigationController = [[UINavigationController alloc] initWithRootViewController:self.exploreViewController]; 
if (tabBarControllerObj!=nil) { 
tabBarControllerObj=nil; 
} 
tabBarControllerObj=[[UITabBarController alloc]init]; 
NSArray *arrayObj=[[NSArray alloc]initWithObjects:self.homeNavigationController,self.accountNavigationController,self.activityNavigationController,self.exploreNavigationController, nil]; 
[tabBarControllerObj setSelectedIndex:0]; 
tabBarControllerObj.tabBar.hidden = YES; 
[tabBarControllerObj setViewControllers:arrayObj]; 
[self.window addSubview:tabBarControllerObj.view]; 
+0

只需通過刪除您的代碼來添加此代碼。刪除添加的最後一個導航控制器。 – Rahul 2013-03-20 06:47:59

+0

問題依然存在。奇怪的是,如果我按下另一個選項卡,然後回去,現在一切正常。我只是不知道爲什麼在另一個選項卡上標籤可以解決這個問題。 – 2013-03-20 07:07:26

+0

它的工作代碼..只是做一個演示,並嘗試這裏的代碼,你會找到解決方案.. – Rahul 2013-03-20 07:32:16

1

發現了問題出在tabbarcontroller的子類,其中

- (void)viewWillAppear:(BOOL)animated 

缺少

[super viewWillAppear:animated]; 
相關問題