以编程方式设置 UITabBarItem 标题?
我的应用程序基于标签栏架构。为了有一个简单的国际化机制,我不在 XIB 文件中写入任何字符串。 viewdidload 允许以编程方式更改视图中的字符串。
当我的应用程序启动时,我可以看到 4 个选项卡。但实际上只有第一个加载其视图控制器。其他等待用户点击加载。 可以使用 [self setTitle:@"Mouhahaha"] 更改选项卡标题;在已加载视图控制器的 viewDidLoad 中。
如果我想保持国际化机制可用,我不会在 XIB 中设置选项卡栏项目的名称。但是,由于在开始时所有选项卡的视图控制器都未加载,因此我的某些选项卡的标题为空白。仅当用户单击选项卡时才设置正确的标题。
我正在寻找一种以编程方式为每个选项卡项目设置此标题的方法。你有提示吗?
多谢。
凯罗
My app is based on a tab bar architecture. In order to have an easy internationalisation mechanic, I do not write any string in XIB files. viewdidload permits to change programmaticaly the strings in the views.
When my app launches, I can see the 4 tabs. But in fact only the first one loads its view controller. The others wait for user click to be loaded.
Tabs title can be changed using [self setTitle:@"Mouhahaha"]; in viewDidLoad of loaded view controller.
If I want to keep my internationalisation mechanic available, I do not set in my XIB the name of tabbar items. But, as at start all tab' view controllers are not loaded, I have blank titles for some tabs. The right title is set only when the user click on the tab.
I am looking for a way to set this title programaticaly for each tabbaritem. Do you have hints ?
Thanks a lot.
kheraud
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我与故事板一起以编程方式执行此操作的首选方法是创建
UITabBarController
的子类,让我的故事板中的选项卡栏控制器场景使用新的子类(带有 3 个UIViewController
在下面的情况下,从选项卡栏控制器到所需视图控制器的关系),然后覆盖viewWillAppear:
my preferred method of doing this programmatically together with the storyboard is to make a subclass of
UITabBarController
, have my tab bar controller scene in my storyboard use the new subclass (with 3UIViewController
relationships from the tab bar controller to the desired view controller in the case below), and then overrideviewWillAppear:
您需要做的就是创建一个 UITabBarController 实例,然后在其中分配您想要的任何视图,然后设置 UITabBarController 视图。确保您的 TabBarController 是可见的。还要确保您想要在选项卡栏中显示的任何 viewController 都已使用
#import
导入。将其放入分配的第一个控制器的 viewDidLoad 方法中。
或者,您可以更改 ApplicationDelegate 设置 TabBarController 的方式,但我不会详细介绍。
希望这有帮助
All you need to do is make an instance of UITabBarController, then alloc any views you want in it, then set the UITabBarController views. Make sure that your TabBarController is the one that is visible. Also make sure that any viewControllers you want in your tab bar are being imported with
#import
.Put this in the viewDidLoad method of the first controller allocated.
Alternatively you could just change the way your ApplicationDelegate sets up the TabBarController, but i won't go into that.
Hope this helps