gpt4 book ai didi

ios - 如何在 iOS 上以编程方式创建选项卡 View

转载 作者:可可西里 更新时间:2023-11-01 04:24:06 26 4
gpt4 key购买 nike

对于 iPhone 应用程序,我如何以编程方式创建选项卡 View ,最好是在 Objective-C 中?

最佳答案

通过 UITabBarController 创建 UITabBar 非常简单.以下示例应该在您的 AppDelegate 类中工作。

应用委托(delegate)接口(interface)

首先,在界面中,我们将定义 UITabBarController .

UITabBarController *tabBarController;

应用委托(delegate)实现

然后,在实现文件的 application:didFinishLaunchingWithOptions: 方法中,我们将初始化我们的标签栏 Controller 。

// Initialise our tab bar controller
UITabBarController *tabBarController = [[UITabBarController alloc] init];

接下来,您需要创建要添加到选项卡栏 Controller 的 View Controller 。我们需要向其中添加一些信息来设置选项卡的标题/图标,但我会在最后回过头来讨论。

// Create your various view controllers
UIViewController *testVC = [[TestViewController alloc] init];
UIViewController *otherVC = [[OtherViewController alloc] init];
UIViewController *configVC = [[ConfigViewController alloc] init];

由于 setViewControllers:animated: 方法需要一个 View Controller 数组,我们将把我们的 View Controller 添加到一个数组中,然后释放它们。 (因为 NSarray 会保留它们。)

// Put them in an array
NSArray *viewControllers = [NSArray arrayWithObjects:testVC, otherVC, configVC, nil];
[testVC release];
[otherVC release];
[configVC release];

然后简单地为 UITabBarController 提供 View Controller 数组并将其添加到我们的窗口。

// Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers animated:NO];

// Put the tabBarController's view on the window.
[window addSubview:[tabBarController view]];

最后,确保在 dealloc 方法中调用 [tabBarController release];

View Controller 实现

在每个 View Controller 中,您还需要在 init 方法中为选项卡设置标题和图标,如下所示:

// Create our tab bar item
UITabBarItem *tabBarItem = [self tabBarItem];
UIImage *tabBarImage = [UIImage imageNamed:@"YOUR_IMAGE_NAME.png"];
[tabBarItem setImage:tabBarImage];
[tabBarItem setTitle:@"YOUR TITLE"];

关于ios - 如何在 iOS 上以编程方式创建选项卡 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5920305/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com