I have UITabBarController
using storyboard and I custom it by another UITabBarController
class in order to perform some changes by requirement of my app.
For my UITabBarController
has 4 child viewControllers. And my first child view I implemented one button in order to change title of TabBarItem
.
This is my custom UITabBarController
class
class TabbarViewController: UITabBarController {
override func awakeFromNib() {
super.awakeFromNib()
}
override func viewDidLoad() {
super.viewDidLoad()
guard let item = tabBar.items else {
return
}
for i in item {
print("This is tabbar item title \(i.title)")
}
}
func language(_ bool: Bool) {
if bool {
print("This is khmer")
self.tabsController?.tabBar.tabItems[0].title = "tab1"
self.tabsController?.tabBar.tabItems[1].title = "tab2"
self.tabsController?.tabBar.tabItems[2].title = "tab3"
self.tabsController?.tabBar.tabItems[3].title = "tab4"
} else {
self.tabsController?.tabBar.tabItems[0].title = "tab5"
self.tabsController?.tabBar.tabItems[1].title = "tab6"
self.tabsController?.tabBar.tabItems[2].title = "tab7"
self.tabsController?.tabBar.tabItems[3].title = "tab8"
}
}
}
I created language
func in order to perform change the title of my tabBaritem
and this func will be called by the first child viewController that I have mentioned above. But I don't know why it returns me nil? But when I list all tabBaritem in viewDidLoad
and it not return nil.
This is first child viewController:
@IBAction func changeLanguageAction(_ sender: UIButton) {
if CustomLocale.shared.LANGUAGE_IDENTIFIER == "EN" {
if let tab = self.tabBarController as? TabbarViewController {
tab.language(true)
}
sender.setImage(R.image.khmerFlag(), for: .normal)
}else {
if let tab = self.tabBarController as? TabbarViewController {
tab.language(false)
}
sender.setImage(R.image.englishFlag(), for: .normal)
}
}
Solved. I just removed this code below in viewDidLoad and pass it to language
func. That means we can call tabBar.items once. That's why it returns me nil when i try to call second time in language
func.
guard let item = tabBar.items else {
return
}