Saturday, January 07, 2012

iOS: UITabBarController


// in root view controller .m

- (void)viewDidLoad
{
    [super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
    UITabBarController *tbc = [[UITabBarController alloc] init];
    
    // create VC instances
    AboutVC *avc = [[AboutVC alloc] initWithNibName:@"AboutVC" bundle:nil];
    ContactVC *cvc = [[ContactVC alloc] initWithNibName:@"ContactVC" bundle:nil];
    
    // set tab bar properties if necessary. use system defined tab if necessary
    UITabBarItem *tbitem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1];
    // link VC to this tab bar item
    [cvc setTabBarItem:tbitem];
    
    // interesting that if title is set in VC, it overrides the title here
    UITabBarItem *tbitem2 = [[UITabBarItem alloc] initWithTitle:@"Hmm" image:[UIImage imageNamed:@"myimage.gif"] tag:2];
    // link VC to this tab bar item
    [avc setTabBarItem:tbitem2];
    
    // create array storing all the VCs
    NSArray *arr = [NSArray arrayWithObjects:avc, cvc, nil] ;
    
    // link array to tab bar controller. if title is set in VC, it will show in tab bar
    [tbc setViewControllers:arr];
    
    // set the view bounds of tab bar
    [tbc.view setFrame:self.view.bounds];
    
    // add tab bar to current view
    [self.view addSubview:tbc.view];
}

No comments: