2013-05-10 52 views
2

我在我的应用程序中有一个标签栏和5个标签。最后一个标签显示了一些广告。我想添加一个设置,用户可以将第5个选项卡关闭......因此,我只是将其从屏幕上移除。有没有办法在iOS中使用应用程序时动态添加标签栏到标签栏?

请注意,我不想隐藏它,我想删除它,以便其余4个选项卡自动均匀分布。

我想要做同样与添加的标签回。

是否有可能做到这一点,而无需重新启动应用程序的用户?

+0

在这里看到一个相关的问题:http://stackoverflow.com/questions/5909727/how-to-remove-programmatically-a-tab-bar-item-created-in-parent-class-nib-file – architectpianist 2013-05-10 00:07:42

回答

6

你只需要使用UITabBarController的viewControllers属性。在需要的时候

NSMutableArray *mutableViewControllers = [tabBarController.viewControllers mutableCopy]; 
[mutableViewControllers removeLastObject]; 
tabBarController.viewControllers = mutableViewControllers; 

使用此代码恢复上次视图控制器:

使用此代码删除最后一个视图控制器

NSMutableArray *mutableViewControllers = [tabBarController.viewControllers mutableCopy]; 
[mutableViewControllers addObject:previouslyRemovedViewController]; 
tabBarController.viewControllers = mutableViewControllers; 

当然这个例子假设你有一个参考到tabBarController,并保持你的lastViewController(例如属性)。

此外,请务必在主线程上运行此代码。

相关问题