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

标签 iphone ios objective-c uitabbarcontroller

我想在我的应用程序中创建一个 tabBar,但只能在详细 View 中创建,而不是在 AppDelegate 中创建。我正在搜索如何在谷歌中执行此操作,但我发现的所有内容都在 AppDelegate.m

我正在尝试做这样的事情。这向我显示了一个带有两个按钮(没有名称)的选项卡栏,但我想从一个带有 tabBar 的 FirstViewController 转到带有导航栏和一个 backItemButton 的两个项目 SecondViewController 或 ThirdViewController 之一,以便返回到 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];
}

最佳答案

至少这对我有用。

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];

关于iphone - 仅以编程方式为不在 AppDelegate 中的一个 ViewController 创建 TabBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18724193/

相关文章:

ios - 静态 NSString 使用与内联 NSString 常量

iphone - 使用 iPhone sdk 编辑 PDF

html - 将字符串编码为 HTML 字符串 Swift 3

iphone - Objective-C 中的 “category” 是什么?

ios - 不允许从场景中删除场景的根节点的 SCNKit 错误

ios - UIImageView 到 UITableViewCell

ios - iOS开发中如何从.a(静态库)中提取文件?

ios - 如何克服 UITableViewCell 子类限制

ios - 如何在特定的 indexpath.row 上显示自定义 tableviewcell

ios - 如何使用 UIPopoverBackgroundView 在 iOS 6 中呈现透明弹出窗口