如何将UITabBarController设置为View?

时间:2012-04-02 12:53:02

标签: iphone objective-c ios xcode uitabbarcontroller

我有一个有多个视图的应用。在一个视图中,我想添加UITabBarController

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

中的.h

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

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

2 个答案:

答案 0 :(得分:3)

在我的一个应用程序中,我使用此代码在app

之间设置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];

答案 1 :(得分:0)

而不是写这个

self.window.rootViewController = self.tabBarController;

在代码中写下:

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

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

希望这可以帮助你.......