gpt4 book ai didi

ios - 在 IOS 中动态添加新标签

转载 作者:行者123 更新时间:2023-11-29 12:11:37 25 4
gpt4 key购买 nike

在 Tab bar Controller 中,最初我在其中添加了 3 个 tab bar 项目。成功登录后,我需要在我当前的 Tab bar Controller 中动态添加一个项目。可以动态添加吗?下面是我试过但不起作用的代码。

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

}

最佳答案

您可以通过编程方式做到这一点:

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

关于ios - 在 IOS 中动态添加新标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33303651/

25 4 0