2012-02-04 50 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];  
    } 
相關問題