2012-02-04 49 views
2

我已经创建了一个tabbar通过笔尖与基于视图的应用程序中的三个项目。
我希望在出现视图时默认选择第一项。在基于视图的应用程序中的Tabbar项不起作用

问题是item1显示选中,但它没有加载它有权做的视图。当我们点击该项目时,视图出现。请帮我解决这个问题。这里是我的代码...

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

回答

1

刚完成它..

-(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];  
    } 
相关问题