NoviceDeveloper
NoviceDeveloper

Reputation: 225

Tabbar item in view based application not working

I have created a tabbar through nib with three items in a view based application.
I want first item get selected by default when the view appear.

The problem is item1 show selected but it doesnt load the view it is entitled to do. when we click on the item the view appears. Please help me to sort out this. here Is my code...

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    tabBar.delegate = self;
    [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
}

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    NSLog(@"didSelectItem: %d", item.tag);
    if (item.tag==1) {
        ImagesOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 210, 320, 250)];
        ImagesOverlay.backgroundColor=[UIColor grayColor];
        [self.view addSubview:ImagesOverlay];
    }else if (item.tag==2) {
        relatedOverlay=[[UIView alloc]initWithFrame:CGRectMake(0, 210, 320, 250)];
        relatedOverlay.backgroundColor=[UIColor redColor];  
        [self.view addSubview:relatedOverlay];
    }else if(item.tag==3){
        //other condition
    }
}

Upvotes: 2

Views: 712

Answers (2)

NoviceDeveloper
NoviceDeveloper

Reputation: 225

Just got it done..

   -(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
tabBar.delegate = self;
[tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];
[self activateTab:1];
 }

- (void)activateTab:(int)index {
switch (index) {
    case 1:
    //condition
    break;
    case 2:
   //condition
    break;
    default:
        break;
}
 }

  - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(@"didSelectItem: %d", item.tag);
[self activateTab:item.tag];    
  }

Upvotes: 1

Ell Neal
Ell Neal

Reputation: 6064

It looks like you need to do some more research into how UITabBarController works. You should be passing it instances of UIViewController, rather than manually changing the views. Have a read of the class reference:

https://developer.apple.com/library/ios/#documentation/uikit/reference/UITabBarController_Class/Reference/Reference.html

Upvotes: 0

Related Questions