如何将导航控制器添加到已编程的选项卡栏

时间:2013-12-09 17:46:41

标签: ios iphone objective-c

您好我有一些代码我正在为iOS 7更新,所以我遇到了问题:iOS 7 Status Bar Collides With NavigationBar

所以我正在努力解决它。在我的RestaurantSearch视图中,我添加了一个导航控制器。问题是现在我的代码崩溃了这个错误:

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

我不知道该怎么设置插座。之前它只是一个视图,它被设置为文件的所有者。那不再是一个选择了。

以下是标签栏的代码

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

崩溃发生在[selfView.navigationController pushViewController:tab animated:YES];

以下是我的导航视图设置的屏幕截图。 Screen Shot

我认为这可能是此问题Loaded nib but the view outlet was not set - new to InterfaceBuilder

的可能重复

但是,当我按照说明操作时,我收到一个新错误:

'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>.'

我的猜测是我错误地挂了一些东西,但我不确定是什么。我将ctr +从导航控制器拖动到上图中的我的视图。这可能与它有关吗?

1 个答案:

答案 0 :(得分:1)

您应该将标签栏和根导航控制器添加到您的应用中。标签栏需要是根视图控制器。您无法将其推送到视图控制器子视图。 您需要在applicationDidFinishLaunching中将其添加到窗口中:方法如:

[self.window setRootViewController:tab];