Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically creating tab bar for ViewController

I've been looking at programatically adding a tab bar to my view controller because having a scroll view I can't place it on without it being in the middle of my view. I'm abit confused about how to add it. Does it need to be initiated in my ViewDidLoad method or my AppDelegate? If I have:

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

How can I allocate it to the bottom of my scrollview?

Thanks!

Now in my appDelegate class :

     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;

}

It's crashing and not sure if it's a release I'm missing.

Edit: For anyone else wondering this in Appdelegate.m:

self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = @[viewController1, viewController2, viewController3, viewController4];
like image 225
Andrea F Avatar asked Jun 11 '13 07:06

Andrea F


Video Answer


1 Answers

Take a look at this Documentation given by Apple: ViewControllers.

Sample Code :

- (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];
}
like image 87
Bhavin Avatar answered Nov 15 '22 12:11

Bhavin