如何设置UITabBarController视图?

我有一个应用程序,有几个意见。 在一个视图中,我想添加一个UITabBarController

在另一个应用程序中,我使用这个将UITabBarController添加到rootViewController ,但我不确定这是否是正确的方法。

在.h

 @property (nonatomic, retain) IBOutlet UITabBarController *tabBarController; 

在他们中

 @synthesize tabBarController=_tabBarController; self.window.rootViewController = self.tabBarController; 

在我的应用程序之一,我已经使用此代码来设置应用程序之间的tabbarcontroller

在AppDelegate.m中定义tabbar控制器

 self.tabBarController = [[UITabBarController alloc] init]; self.tabBarController.delegate=self; self.tabBarController.selectedIndex=0; self.tabBarController.delegate=self; - (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{ //NSUInteger index=[[tabBarController viewControllers] indexOfObject:viewController]; // return YES; } 

在下面的代码中使用标签栏控制器应用您的控制器

 AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; UIViewController *viewController1 = [[GeneralViewController alloc] initWithNibName:@"GeneralViewController" bundle:nil]; UIViewController *viewController2 = [[MiscQuotationController alloc] initWithNibName:@"MiscQuotationController" bundle:nil]; UIViewController *viewController4 = [[QuotationListController alloc] initWithNibName:@"QuotationListController" bundle:nil]; UIViewController *viewController5 = [[ChargesViewController alloc] initWithNibName:@"ChargesViewController" bundle:nil]; UIViewController *viewController7 = [[SalesPartViewController alloc] initWithNibName:@"SalesPartViewController" bundle:nil]; /// tab button title viewController1.title = @"Basic information"; viewController2.title = @"Misc Quotation"; viewController4.title = @"Quotation Line"; viewController5.title = @"Charges"; viewController7.title = @"Sales Part Stock"; // tab button Images viewController1.tabBarItem.image = [UIImage imageNamed:@"general.png"]; viewController2.tabBarItem.image = [UIImage imageNamed:@"misle.png"]; viewController4.tabBarItem.image = [UIImage imageNamed:@"history.png"]; viewController5.tabBarItem.image = [UIImage imageNamed:@"charges.png"]; viewController7.tabBarItem.image = [UIImage imageNamed:@"shoebox.png"]; delegate.tabBarController = [[UITabBarController alloc] init]; delegate.tabBarController.selectedIndex = 0; delegate.tabBarController.viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, viewController4, viewController5, viewController7, nil]; delegate.tabBarController.modalTransitionStyle = UIModalTransitionStyleCoverVertical; [self.navigationController pushViewController:delegate.tabBarController animated:YES]; 

而不是写这个

self.window.rootViewController = self.tabBarController;

写在代码中:

[self.window addSubview:self.tabBarController.view];

您不能将UITabbarController添加到UIViewController ,但可以将UITabBar添加到UIViewController's视图。

希望这可能会帮助你…….