【问题标题】:How to properly navigate from navigationController to tabBarController with it's own navigationControllers如何使用自己的 navigationControllers 从 navigationController 正确导航到 tabBarController
【发布时间】:2011-03-14 04:28:52
【问题描述】:

大家好,

我有一个关于 iPhone 应用程序的一般设计问题。 我想知道如何从带有 navigationController 的普通视图转到带有标签的 tabBarController 的主要原理,其中每个标签都有自己的 navigationController(不再需要第一个 navigationController)。

让我告诉你我是怎么做到的:

首先我向navigationController 添加了一些带有按钮的视图。 AppDelegate 将此导航控制器(当然是视图控制器)作为子视图添加到窗口:

[window addSubview:navigationController.view];

当我到达那个新视图(顶部带有 navigationController)时,我点击按钮,将我带到具有 tabBarController(带有他自己的 navControllers)的新视图:

SearchViewController *searchViewController = [[SearchViewController alloc] initWithNibName:@"SearchView_iPhone" bundle:nil];
searchViewController.tabBarItem.title = @"FirstTab";
UINavigationController *searchNavigationController = [[UINavigationController alloc] initWithRootViewController:searchViewController];

SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsView_iPhone" bundle:nil];
settingsViewController.tabBarItem.title = @"SecondTab";
UINavigationController *settingsNavigationController = [[UINavigationController alloc] initWithRootViewController:settingsViewController];

//Add navigation controllers to tabBar controller
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:searchNavigationController, dictionariesNavigationController, settingsNavigationController, nil];

好的,我将所有视图(带有导航控制器)添加到 tabBarController。我所要做的就是推动 tabBarController 被看到:

[self.navigationController pushViewController:tabBarController animated:YES];

但在那之后,我看到第一个视图中的 navigationController 仍然存在于带有 tabBars 的视图中。这是合乎逻辑的,因为我用 navigationController 推送了 tabBarController。所以我这样做是为了隐藏它:

self.navigationController.navigationBarHidden = YES;

现在看起来没问题。所有的 tabBar 视图都有自己的 navController。

主要问题:当我想从位于 settingsViewController 中的子视图表 (settingsTableViewCell) 推送另一个视图 (settingsResultsViewController) 时,没有任何反应。代码如下:

SettingsResultsViewController *settingsResultsViewController = [[SettingsResultsViewController alloc] initWithNibName:@"SettingsResultsView_iPhone" bundle:nil];
[self.navigationController pushViewController:settingsResultsViewController animated:YES];

我也尝试使用 appDelegate 推送此视图,如下所示:

[delegatePhone.settingsViewController.navigationController pushViewController:settingsResultsViewController animated:YES];
[delegatePhone.firstViewController.navigationController pushViewController:settingsResultsViewController animated:YES];

但还是没有任何反应。

我认为主要问题在于导航控制器。第一个 navigationController 仍然在后面的某个地方,而我想在那个特定的 tabBar 上使用当前的 navController 进行推送。

除了使用 navigationController 之外,有没有办法从第一个视图推送新视图(在我的例子中是 tabBarController)?

我想要的只是当单击 firstView 上的按钮时,该应用程序将我带到 tabBarController 并完全忘记 firstView(和第一个 navigationController) - 我不再需要它们了。

希望我说清楚了。

感谢您的所有帮助。我真的很欣赏它。

【问题讨论】:

  • 我搜索了解决方案,有人说我需要将 tabBarController 设置为根视图控制器。那正确吗?我该怎么做?

标签: iphone uitableview uinavigationcontroller uitabbarcontroller pushviewcontroller


【解决方案1】:

您可以通过调用presentModalViewController:animated: 来显示标签栏控制器,而不是将其推送到导航堆栈。如果您根本不需要第一个导航控制器,您应该将其替换为标准视图控制器。

【讨论】:

  • 感谢您的建议,但我仍然无法在 tabBar 视图的 tableViewCell 中推送另一个视图。当我测试这段代码时,我得到 nil: if (self.navigationController == nil) NSLog(@"navigation is nil"); if (self.parentViewController.navigationController == nil) NSLog(@"父导航为 nil"); self 和 parent navigationControllers 怎么可能是 nil 呢?
【解决方案2】:

Tweetie 作者不久前在一个关于标签栏和中殿控制器的帖子中发表了评论。这个帖子有帮助吗?

Tab bar controller inside a navigation controller, or sharing a navigation root view

【讨论】:

  • 这对我没有帮助。我试图将 tabBarController 推为 modalView (presentModalViewController)。但仍然没有运气。我仍然无法从作为 firstTabBar 子视图的 tableView 推送新视图。谁能帮我解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多