【问题标题】:How to add a Navigation Controller to a programmed tab bar如何将导航控制器添加到编程的标签栏
【发布时间】:2013-12-09 17:46:41
【问题描述】:

您好,我正在为 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];

这是我的导航视图设置的屏幕截图。

我认为这可能与这个问题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+拖动到我的视图。会不会跟这有关系?

【问题讨论】:

  • 我没有使用 StoryBoard,是否可以在不使用 StoryBoard 的情况下使用导航控制器?
  • 链接的解决方案不使用情节提要,它解释了如何以编程方式将导航控制器添加到 tabview 控制器(我隐约记得相反的情况是不可能的?)但是,解决方案“[self.窗口 setRootViewController:tab];"可能就是您所需要的,因为您的错误非常具有描述性,并且准确地说明了问题所在:您有两个视图控制器被此视图使用!

标签: ios iphone objective-c


【解决方案1】:

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

[self.window setRootViewController:tab];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多