简体   繁体   English

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

[英]iPhone app with tab bar and navigation bar as peers

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. 当前,我有一个UINavigationBar(通过UINavigationController)和UITabBar(通过UITabController)作为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. 各个页面的视图控制器被设置为选项卡控制器的viewControllers属性,主页的控制器也被设置为导航控制器的根视图。 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: 选项卡控制器的didSelectViewController委托方法中有以下逻辑:

- (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: 另外,在导航控制器的didShowViewController委托方法中,我有以下代码:

- (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. 编辑:我猜测这与两个控制器都试图拥有页面控制器的所有权有关,但是我无法终生想出一种解决方法。

Yikes. kes。 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. 我已经学到了很难的方式,当您尝试以非标准方式使用UINavigationController和UITabBarController时,它们不太适应。 UINavigationBar and UITabBar weren't designed to coexist as siblings in the view hierarchy. UINavigationBar和UITabBar并非设计为在视图层次结构中作为同级共存。 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? 您是否已实现,并且要从UITabBarControllerDelegate中的tabBarController:shouldSelectViewController:返回YES? 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. 创建一个UITabBarController并将其视图设置为窗口的根视图。

  2. Have a separate UINavigationController under each tab, each with its own navigation bar. 每个选项卡下都有一个单独的UINavigationController,每个选项卡都有自己的导航栏。 (This is the standard way to use UINavigationController and UITabBarController together.) (这是同时使用UINavigationController和UITabBarController的标准方法。)

  3. Set a UITableViewController that displays an identical copy of your main menu table as the root view controller for each navigation controller. 设置一个UITableViewController,它显示与主菜单表相同的副本作为每个导航控制器的根视图控制器。

  4. When a row is selected on your "Home" view controller, switch to the associated content tab. 在“主页”视图控制器上选择一行时,切换到关联的内容选项卡。

  5. Set a delegate on your UITabBarController. 在您的UITabBarController上设置一个委托。 In its tabBarController:didSelectViewController method, call pushViewController:animated:. 在其tabBarController:didSelectViewController方法中,调用pushViewController:animated:。 This is similar to what you're currently doing, except you'll be calling it on (UINavigationController *)tabBarController.selectedViewController. 这与您当前正在执行的操作类似,除了将在(UINavigationController *)tabBarController.selectedViewController上调用它。

  6. In the viewDidAppear:animated: of the root view controllers of your content tabs, switch to the "Home" tab. 在内容选项卡的根视图控制器的viewDidAppear:animated:中,切换到“主页”选项卡。 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. 我很想知道Apple是否接受这种非标准行为。 Come back and let us know. 回来告诉我们。 Also, let us know if you get your approach working somehow. 另外,如果您以某种方式使方法可行,请告诉我们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM