1

Hi I have some code I'm updating for iOS 7, so I have the problem that is outlined here:iOS 7 Status Bar Collides With NavigationBar

So I'm trying to fix it. On my RestaurantSearch View I have added a Navigation Controller. Problem is now my code crashes with this error:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "RestaurantSearch" nib but the view outlet was not set.'

I'm not sure what to set the outlet too. Before when it was just a view it was set to the File's Owner. That's not an option anymore.

Here is my code for the tab bar

void SetTabbar(UIViewController *selfView)
{
UITabBarController *tab = [[[UITabBarController alloc] init] autorelease];

nibName = NIB_NAME(@"RestaurantSearch");
RestaurantSearch *vc1 = [[RestaurantSearch alloc] initWithNibName:nibName bundle:nil];
UINavigationController *nav1 = [[[UINavigationController alloc] initWithRootViewController:vc1] autorelease];
nav1.tabBarItem.title = @"Search";
nav1.tabBarItem.image = [UIImage imageNamed:@"search_on.png"];
[nav1.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"green_search_on.png"] withFinishedUnselectedImage:nil];
nav1.navigationBarHidden = YES;

nibName = NIB_NAME(@"Friends");
Friends *vc2 = [[Friends alloc] initWithNibName:nibName bundle:nil];
UINavigationController *nav2 = [[[UINavigationController alloc] initWithRootViewController:vc2] autorelease];
nav2.tabBarItem.title = @"Friends";
nav2.tabBarItem.image = [UIImage imageNamed:@"contact_on.png"];
[nav2.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"green_contact_on.png"] withFinishedUnselectedImage:nil];
nav2.navigationBarHidden = YES;

nibName = NIB_NAME(@"RewardList");
RewardList *vc3 = [[RewardList alloc] initWithNibName:nibName bundle:nil];
UINavigationController *nav3 = [[[UINavigationController alloc] initWithRootViewController:vc3] autorelease];
nav3.tabBarItem.title = @"Reward";
nav3.tabBarItem.image = [UIImage imageNamed:@"reward_on.png"];
[nav3.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"green_reward_on.png"] withFinishedUnselectedImage:nil];
nav3.navigationBarHidden = YES;

nibName = NIB_NAME(@"MoreViewController");
MoreViewController *vc4 = [[MoreViewController alloc] initWithNibName:nibName bundle:nil];
UINavigationController *nav4 = [[[UINavigationController alloc] initWithRootViewController:vc4] autorelease];
nav4.tabBarItem.title = @"More";
nav4.tabBarItem.image = [UIImage imageNamed:@"more_on.png"];
[nav4.tabBarItem setFinishedSelectedImage:[UIImage imageNamed:@"green_more_on.png"] withFinishedUnselectedImage:nil];
nav4.navigationBarHidden = YES;

// Change the tabbar's background and selection image through the appearance proxy
[[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"tabbar_bg_flat.png"]];
[[UITabBar appearance] setSelectionIndicatorImage:[UIImage imageNamed:@"tabbar_selection_flat.png"]];

NSArray *array = [NSArray arrayWithObjects:nav1, nav2, nav3, nav4, nil];
[tab setViewControllers:array];


// Text appearance values for the tab in normal state
NSDictionary *normalState = @{
                              UITextAttributeTextColor : [UIColor colorWithWhite:0.213 alpha:1.000],
                              UITextAttributeTextShadowColor: [UIColor whiteColor],
                              UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]
                              };

// Text appearance values for the tab in highlighted state
NSDictionary *selectedState = @{
                                UITextAttributeTextColor : [UIColor blackColor],
                                UITextAttributeTextShadowColor: [UIColor whiteColor],
                                UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0, 1.0)]
                                };

[[UITabBarItem appearance] setTitleTextAttributes:normalState forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:selectedState forState:UIControlStateHighlighted];

[selfView.navigationController pushViewController:tab animated:YES];
g_tabbar = tab;
}

The crash occurs on this line [selfView.navigationController pushViewController:tab animated:YES];

Here is a screen shot of my Navigation View setup. Screen Shot

I thought this might be a possible duplicate of this question Loaded nib but the view outlet was not set - new to InterfaceBuilder

However when I follow instructions, I receive a new error:

'A view can only be associated with at most one view controller at a time! View <UIView: 0x1dd45ae0; frame = (0 0; 320 460); autoresize = W+H; autoresizesSubviews = NO; layer = <CALayer: 0x1dd45b40>> is associated with <RestaurantSearch: 0x1dd28e20>. Clear this association before associating this view with <RestaurantSearch: 0x1f03df70>.'

My guess is I'm hooking something up incorrectly, but I'm not sure what. I ctr+dragged from Navigation Controller to my View in the image above. Could this have something to do with it?

Community
  • 1
  • 1
tomjung
  • 715
  • 3
  • 11
  • 34
  • You may want to have a look at http://stackoverflow.com/questions/13835724/programming-tabbarcontroller-with-navigation-in-appdelegate/13839852#13839852 – Pierre-Francoys Brousseau Dec 09 '13 at 18:35
  • I'm not using a StoryBoard, is it possible to use a Navigation Controller without using the StoryBoard? – tomjung Dec 09 '13 at 21:09
  • the link's solution doesn't use storyboard, it explains how to programatically add a navigation controller to a tabview controller (And I vaguely remember the opposite not being possible?) However, the solution "[self.window setRootViewController:tab];" may be all you need since your error is very descriptive and says exactly what is wrong: you have two view controllers being used by this view! – Pierre-Francoys Brousseau Dec 11 '13 at 22:51

1 Answers1

1

You should add tab bar a a root navigation controller to your app. Tab bar needs to be a root view controller. You cannot push it to a view controller subview. You need to add it to your window in applicationDidFinishLaunching: method like:

[self.window setRootViewController:tab];
Greg
  • 25,317
  • 6
  • 53
  • 62