带有标签栏和导航栏的 iPhone 应用程序

发布于 2024-08-28 23:00:52 字数 1377 浏览 2 评论 0原文

我正在尝试编写一个使用导航栏和选项卡栏的应用程序,(我正在收集)这是一种不寻常的方式。

基本上,我有几个“页面”(“主页”、“设置”等),每个页面都有自己的选项卡。我还希望“主页”页面是导航栏的根视图,其他页面是导航栏的二级视图。也就是说,我应该能够通过单击相应的选项卡栏项目导航到任何页面,并且应该能够通过单击导航栏的后退按钮从任何其他页面导航到主页。

目前,我有一个 UINavigationBar (通过 UINavigationController)和一个 UITabBar (通过 UITabController)作为 UIView 的子级。各个页面的视图控制器被设置为选项卡控制器的 viewControllers 属性,主页的控制器也被设置为导航控制器的根视图。每个页面视图的标记都设置为其在选项卡控件中的索引。我在选项卡控制器的 didSelectViewController 委托方法中有以下逻辑:

- (void) tabBarController:(UITabBarController*) tabBarController
    didSelectViewController:(UIViewController*) viewController
{
    if ([navController.viewControllers count] > 1)
        [navController popViewControllerAnimated:NO];

    [navController pushViewController:viewController animated:YES];
}

另外,在导航控制器的 didShowViewController 委托方法中,我有以下代码:

- (void) navigationController:(UINavigationController *) navigationController
    didShowViewController:(UIViewController *)viewController
    animated:(BOOL)animated
{
    tabController.selectedIndex = viewController.view.tag;
}

发生的问题是,当我运行这个,导航栏、选项卡栏和主页都显示正常,但选项卡栏不会响应输入 - 我无法选择不同的选项卡。

我认为将选项卡栏作为导航控件的子项更为常见,反之亦然。这似乎不适合我的方法,因为我不想每次父控件发生更改时都必须单独创建从属控件 - 例如:每次导航栏更改时重新创建选项卡栏。

有人对问题所在以及如何修复有建议吗?我可能错过了一些明显的东西,但无论它是什么,我似乎都找不到它。谢谢!

编辑:我猜这与两个控制器试图拥有页面控制器的所有权有关,但我一生都无法找到解决方法。

I'm trying to write an application that uses a navigation bar and tab bar in what (I'm gathering) is an unusual manner.

Basically, I've got several "pages" ("home", "settings", etc) that each have their own tab. I'd also like to have it so that the "home" page is the root view of the navigation bar, and the other pages are the second-level views of the navigation bar. That is, I should be able to navigate to any page by clicking the appropriate tab bar item, and should be able to navigate to the home page from any other page by clicking the navigation bar's back button.

Currently, I have a UINavigationBar (through a UINavigationController) and a UITabBar (through a UITabController) as children of a UIView. The various pages' view controllers are set as the tab controller's viewControllers property, and the home page's controller is also set as the navigation controller's root view. Each page view's tag is set to its index in the tab control. I have the following logic in the tab controller's didSelectViewController delegate method:

- (void) tabBarController:(UITabBarController*) tabBarController
    didSelectViewController:(UIViewController*) viewController
{
    if ([navController.viewControllers count] > 1)
        [navController popViewControllerAnimated:NO];

    [navController pushViewController:viewController animated:YES];
}

Also, in the navigation controller's didShowViewController delegate method, I have the following code:

- (void) navigationController:(UINavigationController *) navigationController
    didShowViewController:(UIViewController *)viewController
    animated:(BOOL)animated
{
    tabController.selectedIndex = viewController.view.tag;
}

The problem that's occurring is that when I run this, the navigation bar, tab bar and home page all display ok, but the tab bar will not respond to input - I cannot select a different tab.

I gather it's more usual to have the tab bar as the child of the navigation control, or vice versa. This doesn't seem to fit my approach, because I don't want to have to individually create the subordinate control each time a change occurs in the parent control - eg: recreate tab bar each time the navigation bar changes.

Does anyone have suggestions as to what's wrong and how to fix it? I'm probably missing something obvious, but whatever it is I can't seem to find it. Thanks!

EDIT: I'm guessing it has something to do with both controller's trying to have ownership of the page controller, but I can't for the life of me figure out a way around it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睡美人的小仙女 2024-09-04 23:00:52

哎呀。你在这里已经陷入困境了。我经过惨痛的教训才知道,当您尝试以非标准方式使用 UINavigationController 和 UITabBarController 时,它们并不是很方便。 UINavigationBar 和 UITabBar 并不是设计为在视图层次结构中作为同级共存的。他们真的很想成为远房表兄弟。也许您所追求的方法可以以某种方式发挥作用,但我个人甚至不会尝试。

话虽如此,我会建议您可以检查一件事,然后我会告诉您如何解决该问题。

您是否实现了,并且您是否从 UITabBarControllerDelegate 中的 tabBarController:shouldSelectViewController: 返回 YES ?如果没有,这将阻止您的选项卡切换。

假设这不能解决您的所有问题,那么我将采取以下措施来获得您正在寻求的行为:

  1. 创建一个 UITabBarController 并将其视图设置为窗口的根视图。

  2. 每个选项卡下都有一个单独的 UINavigationController,每个选项卡都有自己的导航栏。 (这是一起使用 UINavigationController 和 UITabBarController 的标准方法。)

  3. 设置一个 UITableViewController,它显示与每个导航控制器的根视图控制器相同的主菜单表副本。

    设置

  4. 当在“主页”视图控制器上选择一行时,切换到关联的内容选项卡。

  5. 在 UITabBarController 上设置委托。在其 tabBarController:didSelectViewController 方法中,调用pushViewController:animated:。这与您当前正在执行的操作类似,只不过您将在 (UINavigationController *)tabBarController.selectedViewController 上调用它。

  6. 在内容选项卡的根视图控制器的 viewDidAppear:animated: 中,切换到“主页”选项卡。这样,当用户点击后退按钮返回“主页”时,它将切换到右侧选项卡。 (您可能还需要条件代码来防止用户首次选择内容选项卡时发生这种情况。我不确定。)

我很想知道 Apple 是否接受这种非标准行为。回来告诉我们。另外,如果您的方法以某种方式发挥作用,请告诉我们。

Yikes. You're way out on a limb here. I've learned the hard way that UINavigationController and UITabBarController aren't very accommodating when you try to use them in nonstandard ways. UINavigationBar and UITabBar weren't designed to coexist as siblings in the view hierarchy. They really want to be distant cousins. Maybe the approach you're pursuing can be made to work somehow, but I personally wouldn't even attempt it.

With that said, I'll suggest one thing you can check, and then I'll tell you how I'd approach the problem.

Have you implemented, and are you returning YES from tabBarController:shouldSelectViewController: in your UITabBarControllerDelegate? If not, that would prevent your tabs from switching.

Assuming that won't solve all your problems, here's what I would do to get the behavior you're seeking:

  1. Create a UITabBarController and set its view as the root view of your window.

  2. Have a separate UINavigationController under each tab, each with its own navigation bar. (This is the standard way to use UINavigationController and UITabBarController together.)

  3. Set a UITableViewController that displays an identical copy of your main menu table as the root view controller for each navigation controller.

  4. When a row is selected on your "Home" view controller, switch to the associated content tab.

  5. Set a delegate on your UITabBarController. In its tabBarController:didSelectViewController method, call pushViewController:animated:. This is similar to what you're currently doing, except you'll be calling it on (UINavigationController *)tabBarController.selectedViewController.

  6. In the viewDidAppear:animated: of the root view controllers of your content tabs, switch to the "Home" tab. This way, when the user hits the back button to go back to "Home", it will switch to the right tab. (You may also need conditional code to prevent this from happening when the user first selects a content tab. I'm not sure.)

I'll be curious to know if Apple accepts this nonstandard behavior. Come back and let us know. Also, let us know if you get your approach working somehow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文