将选项卡栏项目添加到 UITabBar 时出现问题
我有一个基于选项卡栏的应用程序,其中我尝试使用 UITabBar
的 setItems
方法动态地将选项卡栏项目添加到选项卡栏。
代码如下:
[self.tabBarController.tabBar setItems:self.level1TabBarItems animated:YES];
其中 self.level1TabBarItems
是一个 NSMutableArray
,其中包含 4 个 UITabBarItems
。 当我运行这段代码时,我从编译器中得到一个异常。
NSInternalInconsistencyException,原因:不允许直接修改由标签栏控制器管理的标签栏。
我尝试删除 UITabBarViewController
并再次添加它,但它不起作用。
I have a tab bar based application in which I am trying to add tab bar items to the tab bar dynamically using setItems
method of the UITabBar
.
Here is the code:
[self.tabBarController.tabBar setItems:self.level1TabBarItems animated:YES];
Where self.level1TabBarItems
is an NSMutableArray
with 4 UITabBarItems
in it.
When I run this code, I get an exception from the compiler.
NSInternalInconsistencyException, reason:Directly modifying a tab bar managed by a tab bar controller is not allowed.
I have tried deleting the UITabBarViewController
and adding it again but it did not work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
文档
明确指出您不应直接修改选项卡栏。使用setViewControllers:animated:
代替。The documentation
clearly states that you shouldn't modify the tab bar directly. UsesetViewControllers:animated:
instead.我希望以下代码可以帮助您:
I hope can help you with following code:
AFAIK 你无法替换标签栏。这是苹果不允许的。我现在就检查一下。
不过,您可以做的是创建一个分段控制器并将其重新设计为看起来像一个选项卡栏。它的用途几乎相同。
编辑:上面,忍者海报说:你不能改变标签栏。我建议使用分段控制器。
AFAIK you can't replace the tabbar. It's not allowed by Apple. I'll check it now.
What you can do though, is creating a segmentedController and restyle it to look like a tabbar. It has pretty much the same use.
EDIT: Above, ninja poster said it: you can't alternate the tabbar. I'd suggest the segmented controller.