任何“非 TabBar 模板”基于如何添加 UITabBar 的教程?

发布于 2024-12-04 06:57:13 字数 207 浏览 1 评论 0原文

我想将 TabBar 添加到我已经启动的现有基于视图的应用程序中,只是为了允许用户切换到应用程序的其他部分,例如“关于”部分和另一个名为“已保存的搜索”的部分来显示导航内容(已保存的搜索列表 > 具体搜索结果 > 产品详细信息)。

关于如何做到这一点有什么想法吗?我找到的所有教程都直接将我指向 TabBar 模板。

感谢您的帮助,

斯蒂芬

I would like to add a TabBar to an existing view-based application I already started just to allow the user to switch to other parts of the app like the "About" section and another section entitled "Saved Searches" to display a navigational content (saved searches list > specific search result > product details).

Any idea on how to do this ? All tutorials I found point me directly to a TabBar template.

Thx for helping,

Stephane

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

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

发布评论

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

评论(2

迷迭香的记忆 2024-12-11 06:57:13

您可以从 UITabBar 应用程序模板开始,您会发现这非常容易做到:

在您的 UIApplicationDelegate 类中,在实例化 UITabBarController 方法中,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

如下所示:

UITabBarController *tabBar = [[UITabBarController alloc] init];

然后您设置将出现在选项卡栏上的视图控制器:

tabBar.viewControllers = viewControllers;

这是一个您之前可以使用 UIViewController 子类创建的 NSArray

NSArray *viewControllers = [[[NSArray alloc] initWithObjects:vc1, vc2, vc3, nil] autorelease];

在此之后,您只需将其设置为窗口的根视图控制器,或将其添加为子视图(它具有相同的效果,但第一种方法在 iOS 4 之前不起作用)

self.window.rootViewController = tabBar;

[self.window addSubView:tabBar.view];

然后

[tabBar release];

要实现您在问题中所说的导航类型,视图控制器你设置的 tabBar 应该是 UINavigationController 的实例,它们很容易像这样创建:

UINavigationController *vc1 = [[UINavigationController alloc] initWithRootViewController:firstViewControllerPage];

在它们内部,你可以推送(导航到)其他视图控制器做:

[self.navigationController pushNavigationController:anotherViewController animated:YES];

希望对它的简短回顾使有点清楚了:)

You could start off with the UITabBar Application Template and you'll realize it's very easy to do:

In your UIApplicationDelegate class, in the method

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

Instantiate a UITabBarController like this:

UITabBarController *tabBar = [[UITabBarController alloc] init];

Then you set the view Controllers that will appear on the tab bar:

tabBar.viewControllers = viewControllers;

Which is a NSArray you can previously create with your UIViewController subclasses:

NSArray *viewControllers = [[[NSArray alloc] initWithObjects:vc1, vc2, vc3, nil] autorelease];

After this, you only have to set it as the root view controller of the window, or add it as a subview (it has the same effect, but the first approach doesnt work prior to iOS 4)

self.window.rootViewController = tabBar;

or

[self.window addSubView:tabBar.view];

And then

[tabBar release];

To achieve the kind of navigation that you say in your question, the view controllers you set to the tabBar should be instances of UINavigationController, which are very easy to create like this:

UINavigationController *vc1 = [[UINavigationController alloc] initWithRootViewController:firstViewControllerPage];

And inside them, you can push (navigate to) other view controllers doing:

[self.navigationController pushNavigationController:anotherViewController animated:YES];

Hope this brief review of it makes it a bit clear :)

酒与心事 2024-12-11 06:57:13

您可以创建一个新的 UITabBarController,并将其视图添加为应用程序窗口的子视图。然后,将其他视图控制器(针对“关于”和“保存的搜索”部分)添加到该选项卡栏控制器。

这可以在 Interface Builder 中最轻松地完成。在 MainWindow.xib 中,将选项卡栏控制器对象拖到画布上。这将自动创建一个包含两个项目的选项卡栏(每个添加的视图控制器一个)。对于选项卡栏控制器下的每个视图控制器,转到身份检查器并将其类更改为自定义视图控制器子类。然后,显示属性检查器,并有一个“NIB 名称”字段 - 再次将其设置为适当的笔尖名称。然后将从相应的 nib 文件加载您的自定义控制器视图。剩下要做的就是在 Interface Builder 中命名每个选项卡,并给它一个图形。

如果您不喜欢 IB,您也可以通过编程方式执行此操作,方法是将自定义视图控制器分配给选项卡控制器的 viewControllers 属性,并分配 selectedViewController

希望这有帮助。

编辑

认为显示一点层次结构可能会有所帮助!您的 MainWindox.xib 结构可能如下所示:

  • AppDelegate
  • UIWindow
  • UITabBarController
    • UITabBar
    • AboutViewController(从“AboutViewController.xib”加载的视图)
      • 选项卡栏项 - 关于
    • UINavigationController
      • 导航栏
      • SavedSearchesViewController - 根视图控制器(从“AboutViewController.xib”加载的视图 ) “SavedSearchesViewController.xib”
      • 选项卡栏项目

并照常从 SavedSearchesViewController 推送适当的视图控制器以提供导航内容。

You can create a new UITabBarController, and add it's view as a subview of your applications window. Then, add your other view controllers (for your "About" and "Saved Searches" sections) to that tab bar controller.

This can be done most easily in Interface Builder. In your MainWindow.xib, drag a Tab Bar Controller object onto the canvas. This will automatically create a tab bar with two items (one for each of the view controllers added). For each view controller under the tab bar controller, go to the identity inspector and change its class to your custom view controller subclass. Then, show the attributes inspector and there is a field "NIB Name" - again, set this to the appropriate nib name. Your custom controller views will then be loaded from their corresponding nib files. All that's left to do is name each tab in Interface Builder, and give it a graphic.

You can also do this programmatically if you don't like IB, by assigning the custom view controllers to the tab controller's viewControllers property, and assign a selectedViewController.

Hope this helps.

EDIT

Thought it might be helpful to show a little hierarchy! Your MainWindox.xib structure might look something like this:

  • AppDelegate
  • UIWindow
  • UITabBarController
    • UITabBar
    • AboutViewController (view loaded from "AboutViewController.xib")
      • Tab Bar Item - About
    • UINavigationController
      • Navigation Bar
      • SavedSearchesViewController - Root View Controller (view loaded from "SavedSearchesViewController.xib"
      • Tab Bar Item

And push appropriate view controllers from SavedSearchesViewController as normal to provide navigation content.

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