应用程序中有 UITabBar 和 UINavigationController 吗?

发布于 2024-08-22 14:37:07 字数 322 浏览 10 评论 0原文

大家好,我是 iPhone 开发新手,我不理解整个 UINavigationController 和 UITabBarController 的想法。其中一种可以替代另一种 - Tweetie 等应用程序如何将两者结合起来?

我想让我的应用程序在底部有一个持久的选项卡栏(这似乎有效),而且在顶部也有一个导航栏,可以将视图推送/弹出到屏幕上,而无需删除选项卡栏。

  • 我怎样才能做到这一点?
  • 对于所有这些控制器,就我的 MainWindow.xib 而言,IB 中的层次结构应该是什么样子?
  • 这里的最佳实践是什么?

非常感谢,

Hey everyone, I am new to iPhone development and I'm not understanding the whole UINavigationController and UITabBarController idea. Is one a substitute for the other - how do apps such as Tweetie combine both?

I'd like to have my app have a persistent Tab Bar @ the bottom (which seems to be working), but also a Navigation bar at the top which can push/pop views onto the screen without removing the tab bar.

  • How can I accomplish this?
  • What should the hierarchy look like in IB as far as my MainWindow.xib with regards to all of these controllers?
  • What is best practice here?

Thanks very much,

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

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

发布评论

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

评论(2

勿忘初心 2024-08-29 14:37:07

只需将视图控制器包装在 UINavigationController 内,并将 UINavigationController 放置在 UITabBar 内。
这对你来说效果很好......

示例:

NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:2];

tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];

UINavigationController *navigationController = nil;
navigationController = [[UINavigationController alloc] initWithRootViewController:<Your View controller1>];
[tabBarViewControllers addObject:navigationController];
[navigationController release];
navigationController = nil;

navigationController = [[UINavigationController alloc] initWithRootViewController:<Your View controller2>];
[tabBarViewControllers addObject:navigationController];
[navigationController release];
navigationController = nil;

tabBarController = tabBarViewControllers;
[tabBarViewControllers release];
tabBarViewControllers = nil;

Just wrap the view controller inside the UINavigationController and Place the UINavigationController inside the UITabBar.
This will work fine for you…

Example:

NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:2];

tabBarController = [[UITabBarController alloc] init];
[tabBarController setDelegate:self];

UINavigationController *navigationController = nil;
navigationController = [[UINavigationController alloc] initWithRootViewController:<Your View controller1>];
[tabBarViewControllers addObject:navigationController];
[navigationController release];
navigationController = nil;

navigationController = [[UINavigationController alloc] initWithRootViewController:<Your View controller2>];
[tabBarViewControllers addObject:navigationController];
[navigationController release];
navigationController = nil;

tabBarController = tabBarViewControllers;
[tabBarViewControllers release];
tabBarViewControllers = nil;
白馒头 2024-08-29 14:37:07

使用选项卡栏应用程序的向导,并正常设置它。在要添加导航控制器的任何选项卡中,使用该库在 XIB 中创建它。我的 XIB 有:

- File's Owner          DescriptiveNameNavViewController
- First Responder
- View                  UIVIew
- Navigation Controller UINavigationController
  - Navigation Bar      UINavigationBar

请注意,视图中没有任何内容。请参阅下面的 viewDidLoad 了解 UINavigationController 附加到 UIView 的位置。

在选项卡的 ViewController 的头文件中(我在这里称之为 DescriptiveNameNavViewController ——对此没有特定的标准,但我使用 [Something]NavViewController 来提醒我这个 ViewController 包含一个带有导航堆栈的导航控制器。这是我在向导生成的 MainWindow.xib 中设置的控制器名称)设置一个 UINavigationController * IBOutlet,它附加了 XIB 中的导航控制器:

@interface DescriptiveNameNavViewController : UIViewController {
    UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

在 DescriptiveNameNavViewController 的控制器中,执行如下操作

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self view] addSubview:[navigationController view]];
    DescriptiveNameController *aController = [[[DescriptiveNameController alloc ] initWithNibName:@"DescriptiveNameController" bundle:nil ] autorelease];
    aController.title = @"Descriptive Title";

//
//  app stuff goes here.
//

    [self.navigationController pushViewController:aController animated:YES];
    [self.navigationController setDelegate:self];
}

: DescriptiveNameNavViewController 中的 delegate 非常重要,因为否则您将无法在 DescriptiveNameViewController 实例中调用您期望的方法以及推送到导航控制器堆栈中的任何其他内容。

在 DescriptiveNameNavViewController 中,实现如下所示的 UINavigationControllerDelegate 方法:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController respondsToSelector:@selector(viewDidAppear:)]) {
        [viewController viewDidAppear:animated];
    }
}

这将导致消息像您期望的那样传播到 UINavigationController 内的控制器。人们遇到的许多问题似乎都是因为 viewDidAppear: 或其他方法没有在推送到 NavigationController 的 ViewController 上被调用。

无论如何,请告诉我更多细节是否有帮助。

Use the wizard for a Tab Bar Application, and set it up as normal. In any tab where you want to add a navigation controller, create it in the XIB using the library. My XIB has:

- File's Owner          DescriptiveNameNavViewController
- First Responder
- View                  UIVIew
- Navigation Controller UINavigationController
  - Navigation Bar      UINavigationBar

Note that there isn't anything in the view. See viewDidLoad below for where the UINavigationController gets attached to the UIView.

In the header file for the Tab's ViewController (which I've here called DescriptiveNameNavViewController -- there isn't a particular standard for this, but I use [Something]NavViewController to remind me that this ViewController contains a navigation controller with the navigation stack. This is the controller name that I set in the MainWindow.xib that the wizard generates) Set up a UINavigationController * IBOutlet that has the navigation controller in the XIB attached to it:

@interface DescriptiveNameNavViewController : UIViewController {
    UINavigationController *navigationController;
}
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;

@end

In the controller for the DescriptiveNameNavViewController , do something like this:

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self view] addSubview:[navigationController view]];
    DescriptiveNameController *aController = [[[DescriptiveNameController alloc ] initWithNibName:@"DescriptiveNameController" bundle:nil ] autorelease];
    aController.title = @"Descriptive Title";

//
//  app stuff goes here.
//

    [self.navigationController pushViewController:aController animated:YES];
    [self.navigationController setDelegate:self];
}

Setting the delegate in the DescriptiveNameNavViewController is super-important, because otherwise you won't get the methods called that you expect in DescriptiveNameViewController instances and anything else you push into the navigation controller's stack.

In DescriptiveNameNavViewController, implement the UINavigationControllerDelegate methods like this:

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    if ([viewController respondsToSelector:@selector(viewDidAppear:)]) {
        [viewController viewDidAppear:animated];
    }
}

And that will cause messages to get propagated to controllers inside the UINavigationController like you expect. It seems like many problems that people encounter are because the viewDidAppear: or other methods aren't getting called on the ViewControllers pushed into the NavigationController.

Anyway, let me know if more detail would help.

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