0

So I have been looking all over the place for examples for how to create a uitabbar prpgrammatically. I tried my best to take what I need from every example and to put together how I want my app to look like: a welcome screen following a 2-tab tabbar.

I have a view controller for the welcome screen with a UIButton to move on:

-(IBAction)aMethod:(id)sender {

    MyTabProjectViewController *controller = [[MyTabProjectViewController alloc] initWithNibName:nil bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    }

Then in my MyTabProjectViewController.m I do this:


    - (void)viewDidLoad
    {
        [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    //Setting up the view
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    contentView.backgroundColor = [UIColor whiteColor];
    self.view = contentView;    

    //Declaring all view controllers
    FirstView *first = [[FirstView alloc] init];
    SecondView *second = [[SecondView alloc] init];

    //Set titles for the view controllers
    first.title = @"First";
    second.title = @"Second";


    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    tabBarController.view.frame = CGRectMake(0, 0, 320, 460);


    UINavigationController *nvc1 = [[UINavigationController alloc] initWithRootViewController:first];
    UINavigationController *nvc2 = [[UINavigationController alloc] initWithRootViewController:second];

    nvc1.navigationBar.barStyle = UIBarStyleBlack;
    nvc2.navigationBar.barStyle = UIBarStyleBlack;


    NSArray *controllers = [[NSArray alloc] initWithObjects:nvc1, nvc2, nil];

    self.tabBarController.viewControllers = controllers;

    [self.view addSubview:tabBarController.view];    
    }

From some reason nothing works. When I click on the button to go to MyTabProjectViewController I see a blank page.

phihag
  • 278,196
  • 72
  • 453
  • 469
TommyG
  • 4,145
  • 10
  • 42
  • 66

2 Answers2

0

try adding

tabBarController.selectedIndex = 0;
Stas Zhukovskiy
  • 799
  • 9
  • 21
0

You instantiating a new instance of the UITabBarController and setting the ViewControllers list to another instance, just replace the line:

self.tabBarController.viewControllers = controllers;

with the line:

tabBarController.viewControllers = controllers;
Mousa
  • 2,926
  • 1
  • 27
  • 35