I will miss access to storyboard If I define UITabBar programmatically! [SOLVED]

Fist I’m sorry for my English

I’m starting my first project for iDevices, But I have a lot of issues :confused:

I’m create a TabBarController.swift class for customizing:

`class TabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
delegate = self
}

override func viewWillAppear(animated: Bool) {  

    let home = HomeController()  
    let service = ServiceViewController()  
    let home2 = Home2Controller()  
    let service2 = Service2ViewController()  
            

    let homeIcon = UITabBarItem(title: "Home", image: nil, selectedImage: nil)  
    let serviceIcon = UITabBarItem(title: "Service", image: nil, selectedImage: nil)  
    let homeIcon2 = UITabBarItem(title: "Home2", image: nil, selectedImage: nil)  
    let serviceIcon2 = UITabBarItem(title: "Service2", image: nil, selectedImage: nil)  
    


    home.tabBarItem = homeIcon  
    service.tabBarItem = serviceIcon  
    home2.tabBarItem = homeIcon  
    service2.tabBarItem = serviceIcon  

    let tabBarController = [home, service, home2, service2]  
    self.viewControllers = tabBarController  
}  

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {  
    return true  
}  

} `

I made 4 ViewController.swift class like this:

import UIKit class HomeViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = UIColor.greenColor() self.title = "Home" print("Home Page") / } }

in storyboard:

Storyboard Image:

Root: a tab tab controller
first row: 4 navigation controller
second row: 4 view controller

I set all view controllers identify inspect to self view controller class.
I set tabBarController identify inspect class to UITabBarController class.

my problem appear after running this app, I except to see this black/ gray/ green/ blue views, but I didn’t.
after some changes I found out that views I designed in storyboard will not showed.
So I made a navigationController class and set navigation controller identify inspector to this class and changed TabBarController class to work with navigationController class instead of view controllers. after running navigation appeared but still now view from storyboard!!!
I’m just confused! Why xCode dont understand connection between this views from storyboard ?!!
It’s gone take a lot of time If I defined all views in code.

[quote=“vahids, post:1, topic:7786”]
let home = HomeController() let service = ServiceViewController() let home2 = Home2Controller() let service2 = Service2ViewController() //... let tabBarController = [home, service, home2, service2] self.viewControllers = tabBarController[/quote]
Here’s your problem. Your UITabViewController was created by the storyboard, with four coloured view controllers in navigation controllers, and then in code you built four new view controllers and replaced the ones created from the storyboard.
I’ll assume for the moment that those four coloured view controllers will be the only ones of their kind. UIViewController subclasses have a tabBarItem property, as you’ve noticed. You could just write, in the viewDidLoad() of your HomeController class, tabBarItem = UITabBarItem(title: "Home", image: nil, selectedImage: nil)? That way the controller created by the storyboard will set its own tab icon.
That’s the simple way, with that assumption. If you have other reasons to create a subclass of UITabController, you should know that UIViewController subclasses also have a tabController property, which is set to the UITabController (or its subclass) it’s embedded in, if you want to access the TabController more directly.
If not all HomeControllers want that tab icon, or you genuinely want the tab controller to be responsible for its icons (not an unreasonable idea), then it gets a bit complicated because of the timing of embedding segues, so I would think you’d have to write something in the prepareForSegue(_:sender:) function.
Does that help?

1 Like

Is this way OK on standards?
I’m trying to write codes classify and follow standard.

I’m not sure what you mean by standards.
Apple’s API for the UIViewController class is that a view controller is aware of whether it’s embedded in a UITabController (even if also embedded in a UINavigationController) and can set its own tabBarItem. So your HomeController can set its own tabBarItem and that’s consistent with Apple’s design.
If you’re referring to principles such as the Single Responsibility Principle (one of the SOLID principles), then you could argue that the UITabController ought to be responsible for the tab icons of its view controllers, and the view controllers shouldn’t know about them. On the other hand it could also be argued that each view controller should be in control of its tab icon and the tab controller should not be responsible for any of the icons. Both would be valid interpretations of the principle, but Apple seem to have decided that the view controller should be responsible.

1 Like

I mean MVC standards.
BTW, thanks for your help.

I put this line
tabBarItem = UITabBarItem(title: "Home", image: nil, selectedImage: nil)?
to HomeController but not worked so I make a new class for HomeNavigarionController and put this line on viewDidLoad of HomeNavigarionController class and everything working well now for home tab, but for other tabs Item names will be change after I click on tab bar.
Items instead the Home, (cause it’s loaded) names are “item” and If I tap them their name will be changed to excepted name

------------------- EDIT
I put that line code to init of navigation controllers.
So I think I should mark this topic as SOLVED.