简体   繁体   中英

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
    }
}

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];    
  }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM