以编程方式仅为一个不在AppDelegate中的ViewController创建TabBar

时间:2013-09-10 16:23:04

标签: iphone ios objective-c uitabbarcontroller

我想在我的应用中创建一个tabBar,但只能在不在AppDelegate中的detailView中创建。我正在谷歌搜索如何做到这一点,但我所知道的一切都在AppDelegate.m

我正在尝试做这样的事情。这向我展示了一个带有两个按钮(没有名称)的Tab栏,但我想从一个带有tabBar的FirstViewController转到两个项目SecondViewController或ThirdViewController中的一个带有一个带有一个backItemButton的navigationBar,以便返回到FirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setupTabBar];
}
- (void) setupTabBar {

    tabBars = [[UITabBarController alloc] init];
    NSMutableArray *localViewControllersArray = [[NSMutableArray alloc] initWithCapacity:4];


    YPProfileViewController *profile = [[YPProfileViewController alloc] init];
    YPProfileViewController *profile2 = [[YPProfileViewController alloc] init];

    [localViewControllersArray addObject:profile];
    [localViewControllersArray addObject:profile2];


    tabBars.tabBarItem = profile2;
    tabBars.viewControllers = localViewControllersArray;
    tabBars.view.autoresizingMask==(UIViewAutoresizingFlexibleHeight);


    [self.view addSubview:tabBars.view];
}

1 个答案:

答案 0 :(得分:4)

至少这对我有用。

UIViewController *vc1 = [[UIViewController alloc] init];
vc1.title = @"FIRST";
vc1.view.backgroundColor = [UIColor blueColor];

UIViewController *vc2 = [[UIViewController alloc] init];
vc2.title = @"SECOND";
vc2.view.backgroundColor = [UIColor redColor];

UITabBarController *tabBar = [[UITabBarController alloc] init];
tabBar.viewControllers = @[vc1,vc2];
tabBar.selectedIndex   = 1;
tabBar.view.frame = CGRectMake(50, 50, 220, 320);

[tabBar willMoveToParentViewController:self];
[self.view addSubview:tabBar.view];
[self addChildViewController:tabBar];
[tabBar didMoveToParentViewController:self];