0

I am encountering a very strange problem.

Here is the code structure in my app delegate:

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];

for that self.navController, I define that like this

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

so after the tabbarcontroller is presented, i will be at the homeviewcontroller where i have a collectionview. it successfully display multiple cell. When I click on the cell,

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

i see that the navigation bar is changed with the new title in "vc", and the navigation bar's back button also show up. However, the view is not changed, i am still able to access the collectionview.

however, if i pressed on other tab on the tabbarcontroller such as the accountviewcontroller,and then press back to the old tab for homeviewcontroller again, the view will now show up. this is a very weird behavior that i have no idea why it happens.

i also checked that in each of the view controller, the self.navigationcontroller is not nil

Vincent Yiu
  • 1,291
  • 2
  • 11
  • 14
  • You need to describe your app structure more thoroughly. Did you make the controllers in a storyboard? In code? – rdelmar Mar 20 '13 at 05:53
  • There was a very similar question about a week ago which never got answered. You're not going to get any useful help without fully describing your setup. – rdelmar Mar 20 '13 at 06:20
  • ok, updated with code structure – Vincent Yiu Mar 20 '13 at 06:29
  • Where and how do you create the controller you're pushing, vc? – rdelmar Mar 20 '13 at 06:37
  • I can't tell from what you've posted why this doesn't work. What changes and/or additions did you make to UITabBarController in MyOwnTabBarController? I think in general, it's not a good idea to embed your tab bar controller in a navigation controller (I noticed in IB that the option to embed in a navigation controller is grayed out for a tab bar controller). Better to just present the welcome controller from the first tab I think. – rdelmar Mar 20 '13 at 06:46
  • vc is created just like other viewcontroller using initwithnibname – Vincent Yiu Mar 20 '13 at 07:16

3 Answers3

1

Found out the problem is at the subclass of tabbarcontroller where

- (void)viewWillAppear:(BOOL)animated

is missing

[super viewWillAppear:animated];
Vincent Yiu
  • 1,291
  • 2
  • 11
  • 14
0

As i understand. Just check if u have added the navigationController to tabBar. Check it below. If not Convey me the total scenario.

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];
Rahul
  • 134
  • 6
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];
Rahul
  • 134
  • 6
  • Just add this code by removing yours. Remove the last navigation controller u added. – Rahul Mar 20 '13 at 06:47
  • the problem still exists. What so strange is, if i press on one of the other tab, and then go back, now everything is working fine. I just don't know why tabbing on the other tab can resolve this. – Vincent Yiu Mar 20 '13 at 07:07
  • Its the working code.. Just make a demo and try this code there u will find the solution.. – Rahul Mar 20 '13 at 07:32