通过appDelegate.m中的代码嵌入导航控制器而不是“菜单:编辑器 - >嵌入 - >导航控制器”

时间:2015-01-22 09:43:50

标签: ios objective-c

我通过appDelegate.m中的代码创建tabbar控制器并设置tabbar控制器是rootViewController.So,在main.storyboard中我没有指向导航控制器的指针,我想按代码设置tabbarcontronller的导航控制器,我怎样才能做到这一点? 如果我没有设置导航控制器,它将抛出异常“'推送segues只能在源控制器由UINavigationController实例管理时使用。'”

NSArray *titleList = @[@"Bán chạy",@"Mới nhất",@"Lịch sử",@"Khách hàng"];
    NSArray *imageList = @[@"icon_hot_normal.png",@"icon_new_normal.png",@"icon_history_normal.png",@"icon_user_normal.png"];

    NSArray *viewCtrIdentifierList = @[@"best_sale_id",@"history_id"];

    UITabBarController *tabbarCtr = [[UITabBarController alloc]init];

    self.window.rootViewController = tabbarCtr;
    [tabbarCtr navigationController];
    NSMutableArray *tabbarList = [[NSMutableArray alloc]init];
    for (int i=0; i < viewCtrIdentifierList.count; i++) {
        UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

        UIViewController *viewCtr = [storyBoard instantiateViewControllerWithIdentifier:viewCtrIdentifierList[i]];
        [tabbarList addObject:viewCtr];
    }

    tabbarCtr.viewControllers = tabbarList;
    int i=0;
    for (UITabBarItem *item in tabbarCtr.tabBar.items) {
        item.title = titleList[i];
        item.image = [UIImage imageNamed:imageList[i]];
        i++;
    }

1 个答案:

答案 0 :(得分:1)

此代码应该是您走上正确轨道所需的代码。

基本上,您需要将每个选项卡的根视图控制器包装在UINavigationController中。你非常接近。

此代码应解决您的问题:

NSArray *viewCtrIdentifierList  = @[@"best_sale_id",@"history_id"];
UITabBarController *tabbarCtr   = [[UITabBarController alloc] init];
self.window.rootViewController  = tabbarCtr;
NSMutableArray *tabbarList      = [[NSMutableArray alloc]init];
for (int i=0; i < viewCtrIdentifierList.count; i++) {
    UIStoryboard *storyBoard    = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *viewCtr   = [storyBoard instantiateViewControllerWithIdentifier:viewCtrIdentifierList[i]];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewCtr];
    [tabbarList addObject:nav];
}
tabbarCtr.viewControllers = tabbarList;