在IOS中动态添加新选项卡

时间:2015-10-23 13:33:12

标签: ios iphone uitabbarcontroller ios9 tabbar

在标签栏控制器中,最初我在其中添加了3个标签栏项目。成功登录后,我需要在我当前的标签栏控制器中动态添加一个项目。是否可以动态添加?下面是我尝试过的代码,但它没有用。

 if (AppContext.loginUser.userId!=nil) {
        UITabBarItem *tabBarItem4;// = [tabBar.items objectAtIndex:3];
        tabBarItem4.selectedImage = [[UIImage imageNamed:@"SelectedProfileTab"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
        tabBarItem4.image = [[UIImage imageNamed:@"Profiletab"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
        tabBarItem4.title = @"Profile";
        tabBarItem4.tag =4;

        ProfileVC *profile = loadViewController(TabbarSB, VC_Profile);

        UINavigationController *nv=[[UINavigationController alloc] initWithRootViewController:profile];

        NSMutableArray *arrayOfTabBars=[AppContext.mainTabbar.viewControllers mutableCopy];
        [arrayOfTabBars addObject:nv];


       // [AppContext.mainTabbar setViewControllers:nil];

        [AppContext.mainTabbar setViewControllers:arrayOfTabBars];


        //AppContext.mainTabbar.viewControllers = arrayOfTabBars;
      // [self.tabBarController.tabBarController addChildViewController:profile];

    }

1 个答案:

答案 0 :(得分:0)

您可以通过编程方式执行此操作:

if (AppContext.loginUser.userId!=nil) {
    // First, create your view controller
    ProfileVC *profile = loadViewController(TabbarSB, VC_Profile);

    // then embed it to a navigation controller
    // this is not required, only if you need it
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:profile];

    // Get viewControllers array and add navigation controller
    NSMutableArray *viewControllers = [NSMutableArray arrayWithArray:self.tabBarController.viewControllers];
    [viewControllers addObject:nav];

    // Set back the array
    [self.tabBarController setViewControllers:viewControllers];

    // Create tab bar item for ProfileVC
    profile.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:image selectedImage:selectedImage];
}