以编程方式为ViewController创建选项卡栏

6

我一直在研究如何在我的视图控制器中以编程方式添加选项卡栏,因为如果我不使用滚动视图,就无法将其放在视图的中间。 我对如何添加它感到有些困惑。它需要在我的ViewDidLoad方法或AppDelegate中初始化吗? 如果我有:

UITabBarController  *tabBar = [[UITabBarController alloc] init];
[self.view addSubview:tabBar.view];
[tabBar release]; 

如何将它分配到我的scrollview的底部?

谢谢!

现在在我的appDelegate类中:

     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    ViewController* vc = [[ViewController alloc] init];


    NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil];
    tabBarController.viewControllers = controllers;

    [_window addSubview:tabBarController.view];

   self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
   return YES;

}

它崩溃了,我不确定是否缺少某个版本的发布。

编辑:对于其他人想知道的,在Appdelegate.m文件中:

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4];

NSArray* controllers = [NSArray arrayWithObjects:vc, tabBarController, nil]; 你为什么要添加 tabBarController - Bhavin
是的,我在我的问题中添加了“编辑”以展示对我有效的解决方案。 - Andrea F
还有一件事情,就是:你把 tabBarController 添加到了你的 viewControllers 数组中,这是错误的。这就是我在我的答案中添加的内容。 - Bhavin
3个回答

8
请查看由苹果提供的文档:视图控制器示例代码:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
   UITabBarController *tabBarController = [[UITabBarController alloc] init];

   FirstViewController* vc1 = [[FirstViewController alloc] init];
   SecondViewController* vc2 = [[SecondViewController alloc] init];

   NSArray* controllers = [NSArray arrayWithObjects:vc1, vc2, nil];
   tabBarController.viewControllers = controllers;

   // Add the tab bar controller's current view as a subview of the window
   [window addSubview:tabBarController.view];
}

只有一件微不足道的小事情:[self.window addSubview:tabBarController.view]; - Ben

3
我是一名有用的助手,以下是您需要翻译的内容:

我是用这种方法实现的。我从我的应用程序委托中复制了这个方法,并且它能够很好地工作。基本上,您需要将视图控制器添加到选项卡栏中,然后将选项卡栏添加到窗口的子视图中。

实例化实例

iVacationTabBar = [[UITabBarController alloc] init];

无论您如何创建视图/视图控制器:
 UINavigationController *rvc_tools = [[UINavigationController alloc] initWithRootViewController: vc_tools];
    UINavigationController *rvc_settings = [[UINavigationController alloc] initWithRootViewController: vc_settings];
    UINavigationController *rvc_about = [[UINavigationController alloc] initWithRootViewController: vc_about];
    UINavigationController *rvc_currentpage = [[UINavigationController alloc] initWithRootViewController: vc_currentpage];
    UINavigationController *rvc_backup = [[UINavigationController alloc] initWithRootViewController:vc_backup];

将控制器添加到数组中:
NSArray* controllers = [NSArray arrayWithObjects: rvc_currentpage,  rvc_tools,rvc_settings, rvc_backup, rvc_about, nil];

将视图控制器数组设置为您的选项卡栏:

[iVacationTabBar setViewControllers: controllers animated:NO];

将选项卡栏添加到窗口的子视图中。
[_window addSubview:iVacationTabBar.view];

0
你不能在一个 UIViewController 上添加一个 UITabbarController。相反,在一个 ViewController 上,你必须展示 TabbarController,创建一个 TabbarController,为它设置 ViewController,并将其添加到一个 Window 中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接