以编程方式在Swift中设置tabBarItem标题 [英] Programmatically setting tabBarItem title in Swift

查看:406
本文介绍了以编程方式在Swift中设置tabBarItem标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有四个UIViewControllers链接到UITabBarController的标签栏。我需要在故事板之外和课堂内设置标签栏项目标题。

I have four UIViewControllers that are linked to a UITabBarController's tab bar. I need to set the tab bar item titles outside of the storyboard, and inside of their classes.

我试过..

class MyViewController: UIViewController {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        self.title = NSLocalizedString(MyConstants.StringKeys.TabName, tableName: Constants.Strings.MyTable, comment: Constants.EmptyString);
    }
}

这是被调用的,但标题永远不会设置。与self.tabBarItem.title =标题相同

This is called, but the title is never set. Same with self.tabBarItem.title = "the title"

我也尝试在viewDidLoad中设置标题,但这只是在转到视图控制器后更新标题。

I've also tried setting the title in viewDidLoad, but that only updates the title after going to the view controller.

想法?

推荐答案

我想通了,看起来像它被awakeFromNib()覆盖。

I figured it out, looks like it was being over written by awakeFromNib().

override func awakeFromNib() {
    super.awakeFromNib()

    self.title = NSLocalizedString(MyConstants.StringKeys.TabName, tableName: Constants.Strings.MyTable, comment: Constants.EmptyString);
}

我在那里移动了我的self.title作业并纠正了我的问题。

I moved my self.title assignment there and it corrected my issue.

这篇关于以编程方式在Swift中设置tabBarItem标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆