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];
4

3 回答 3

8

查看 Apple 提供的文档:ViewControllers

示例代码:

- (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];
}
于 2013-06-11T07:53:14.773 回答
3

我是这样做的。我从我的 App Delegate 复制了这个,它工作正常。基本上,您将视图控制器添加到选项卡栏,然后将选项卡栏添加到窗口的子视图。

实例化实例

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];
于 2013-06-11T07:55:05.283 回答
0

您不能在 aUITabbarController上添加 a UIViewController。相反,ViewController您必须在 a 上显示TabbarController、创建 a TabbarController、为其设置ViewControllers 并将其添加到 a Window

于 2013-06-11T08:00:23.387 回答