【问题标题】:App with UINavigationController and UITabBarController带有 UINavigationController 和 UITabBarController 的应用程序
【发布时间】:2012-10-13 12:15:10
【问题描述】:

我已经为这个问题苦苦挣扎了一段时间。我让它工作了,但不是它应该的方式,我猜是因为我做错了。我的目标是拥有以下布局。

我有一个带有四个按钮的主菜单。单击每个会显示不同的 TableView(我应该在 navController 的右上角有一个后退按钮,以便能够返回主菜单,但我没有)。这些 TableView 中的每一个都在一个 UITabBarController 内(看起来像这样 http://i.stack.imgur.com/GkFwo.jpg

那么我该怎么做呢?

这是我当前的代码(在某些时候肯定是错误的)

- (IBAction)MyButton:(id)sender
{
self.tabBarController = [[UITabBarController alloc] init];
UIViewController *viewController1, *viewController2, *viewController3, *viewController4;
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    viewController1 = [[[LexikonControllerViewController alloc] initWithNibName:@"LexikonControllerViewController" bundle:nil] setArrays:PflanzenIDs List:ListOfPlants Index:IndexOfPlants dict:DictionaryOfPflantsAndIDs];
}

UINavigationController *navController1 = [[UINavigationController alloc] initWithRootViewController:viewController1];

NSArray* controllers = [NSArray arrayWithObjects:navController1, nil];

self.tabBarController.viewControllers = controllers;

CGRect frame = self.tabBarController.view.frame;
frame.size.height = frame.size.height - 20;
self.tabBarController.view.frame = frame;

[self.view addSubview:self.tabBarController.view];
}

编辑:

现在我尝试了这个: - (void)applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

vc = [[MyViewController alloc] init];
[vc setTitle:@"Home"];
[vc setParent:self];

navController = [[UINavigationController alloc] initWithRootViewController:vc];

UITabBarController *tb = [[UITabBarController alloc] init];
NSArray *controllers = [[NSArray alloc] initWithArray:[NSArray arrayWithObjects:navController, nil]];
tb.viewControllers = controllers;

[window addSubview:[tb view]];
[window makeKeyAndVisible];
}

问题:在主视图(带有主菜单)上,我可以看到标签栏(不应该在那里)并且使用 hidesBottomBarWhenPushed 在所有其他视图上完全隐藏标签栏:(

【问题讨论】:

    标签: objective-c uinavigationcontroller uitabbarcontroller


    【解决方案1】:

    标签栏控制器应该是您的根视图控制器。您也可以将其连接到情节提要中。

    【讨论】:

    • 但不是这样吗?你能更具体一点(使用我的代码)吗?谢谢
    • 您在使用情节提要吗?如果是这样,您不需要分配/初始化您的 VC,我认为情节提要应该为您执行此操作。除非您尝试做一些时髦的事情,否则您也不应该弄乱窗口 - 这可能是错误的轨道。查看 Apple Developer 文档以获取故事板和视图控制器,或故事板教程,例如 raywenderlich.com/5138/beginning-storyboards-in-ios-5-part-1
    【解决方案2】:

    成功了。我所做的是将我的主菜单设置为导航控制器的根视图,如果我单击其中一个按钮,我会加载一个新的 UIViewController,它负责设置 TabBarController 并在每个选项卡中加载我的四个不同的表格视图。 所有这一切都没有故事板和 XIB。

    【讨论】:

    • 嗨丹尼尔,请问可以粘贴 AppDelegate 的代码吗?看看 NavigationController 是如何设置为控制几个不同 UITabBarControllers 的 rootViewController 的吗?非常感谢!
    • 我和@lionfly 的理解不一样。
    猜你喜欢
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 2013-12-18
    • 1970-01-01
    相关资源
    最近更新 更多