2012-02-14 59 views
1

我想建立一個自定義標籤欄控制器,但由於某些原因,視圖不會切換...初始視圖加載正常。這裏是我的init方法:自定義標籤欄控制器不工作?

- (id)initWithNibName:(NSString *)nibNameOrNil 
      bundle:(NSBundle *)nibBundleOrNil 
{ 
AccountViewController *accountViewController = [[AccountViewController alloc] 
        initWithNibName:@"AccountViewController" bundle:nil]; 
MoreViewController *moreViewController = [[MoreViewController alloc] 
        initWithNibName:@"MoreViewController" bundle:nil]; 
BarTabViewController *barTabViewController = [[BarTabViewController alloc] 
        initWithNibName:@"BarTabViewController" bundle:nil]; 
LocationsViewController *locationsViewController = [[LocationsViewController alloc] 
        initWithNibName:@"LocationsViewController" bundle:nil]; 

self.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController, 
         barTabViewController, moreViewController, nil]; 

[self.view addSubview:locationsViewController.view]; 
self.selectedController = locationsViewController; 

    return self; 
} 

就像我說的,這會正確顯示所選擇的控制器,但是當應用程序啓動,我嘗試切換視圖與標籤欄,子視圖只是變成灰色......我一直在瀏覽幾個教程來試圖解決這個問題,但看起來我正在做的完全一樣。我也檢查了IB文件以確保我的標籤連接正確,他們是。下面是切換項目代碼:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item 
{ 
    if (item == locationsTabBarItem) { 
     UIViewController *locationsController = [viewControllers objectAtIndex:0]; 
     [self.selectedController.view removeFromSuperview]; 
     [self.view addSubview:locationsController.view]; 
     self.selectedController = locationsController; 
    } 
    else if (item == accountsTabBarItem) { 
     UIViewController *accountsController = [viewControllers objectAtIndex:1]; 
     [self.selectedController.view removeFromSuperview]; 
     [self.view addSubview:accountsController.view]; 
     self.selectedController = accountsController; 
    } 
    else if (item == barTabTabBarItem) { 
     UIViewController *barTabController = [viewControllers objectAtIndex:2]; 
     [self.selectedController.view removeFromSuperview]; 
     [self.view addSubview:barTabController.view]; 
     self.selectedController = barTabController; 
    } 
    else { 
     UIViewController *moreController = [viewControllers objectAtIndex:3]; 
     [self.selectedController.view removeFromSuperview]; 
     [self.view addSubview:moreController.view]; 
     self.selectedController = moreController; 
    } 
} 
+0

你是不是在您的視圖標籤欄控制器整合 – Hiren 2012-02-14 05:59:50

回答

2

嘗試用這一個

self.tabBarController = [[UITabBarController alloc] init]; 
    self.tabBarController.selectedIndex = 0; 

    self.tabBarController.viewControllers = [NSArray arrayWithObjects:locationsViewController, accountViewController, 
         barTabViewController, moreViewController, nil]; 
    self.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; 
    [self.navigationController pushViewController:delegate.tabBarController animated:YES];