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

And*_*a F 6 iphone objective-c

我一直在寻找以编程方式向我的视图控制器添加一个标签栏,因为有一个滚动视图我无法将它放在我的视图中间.我对如何添加它很困惑.是否需要在我的ViewDidLoad方法或我的方法中启动AppDelegate?如果我有:

UITabBarController  *tabBar = [[UITabBarController alloc] init];
[self.view addSubview:tabBar.view];
[tabBar release]; 
Run Code Online (Sandbox Code Playgroud)

我怎样才能将它分配到我的底部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;

}
Run Code Online (Sandbox Code Playgroud)

它崩溃了,不确定它是否是我失踪的版本.

编辑: 对于其他人在Appdelegate.m中想知道这个:

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4];
Run Code Online (Sandbox Code Playgroud)

Bha*_*vin 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];
}
Run Code Online (Sandbox Code Playgroud)