Jeesson_7
Jeesson_7

Reputation: 811

Can I use the same ViewController to be added multiple times in TabBarController

I has a tabBar controller which contain 5 items. The first three items use the same viewController as the UI design and functionality is almost the same. So I made a single View Controller for first three items. But I couldn't figure out how to add the same viewController multiple times in a tabBar Controller. Is it possible or against the apple rules?

Upvotes: 2

Views: 1919

Answers (2)

Ali Beadle
Ali Beadle

Reputation: 4516

You can link a tab bar controller to a view multiple times: the result is multiple tabs on the tab bar that link to the same GUI but the controller is instantiated once for each version - i.e. they are each controlled by a different view controller of the same type.

To do this:

  • Open the Document Outline in storyboard if it is not already open.
  • Right-click your tab bar controller and drag down to the view that you wish to be the tab bar item and release.

enter image description here

  • In the popup menu that appears, select the type of relationship as 'view controllers'.

enter image description here

  • Repeat as many times as you need duplicate tab bar items.

However, this does restrict these tab bar items to have exactly the same GUI. If you do want a slightly different GUI but avoid code duplication, you can create different views for each and then use the same UIViewController class for each view (this works for views in general, not just TabBarItems).

In the storyboard you can do this by:

  • Selecting the item in storyboard,
  • Opening the 'Utilities' sidebar on the right-hand side if it is not already open,
  • Open the 'Identity Inspector',
  • In the 'Custom Class', 'Class' picker, select your class name (the class type must match the type of view you are trying to link it to).

enter image description here

You will need to link any controls that need outlets or actions from the storyboard to the code for each view that shares this controller.

It is important to note that a new instantiation of this class will be created for you for each view that you link to your class (i.e. you are linking the view to the same type of the controller, not the same controller). So if you need to share data or state information between your view controllers you will have to pass this to them using the normal mechanisms.

Upvotes: 2

amar
amar

Reputation: 4345

@Ali Beadles answer is correct. I am just presenting an alternate approach.

As mentioned in the above answer you can drag and drop to create multiple relationship segues to the same ViewController. It will create multiple tab bar items and each will instantiate a new instance of same view controller. In order to customize tab bar items subclass your UITabBarController.

    class YourTabViewController: UITabBarController {
        override func viewDidLoad() {
            super.viewDidLoad()
            setTabBarItem()
        }
        func setTabBarItem(){
//supposing you have only 2 tabbar items add as many as you need
            let myTabBarItem0 = (self.tabBar.items?[0])! as UITabBarItem
            myTabBarItem0.image = UIImage(named: "Icon_Inbox")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            myTabBarItem0.selectedImage = UIImage(named: "Icon_Inbox ")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            myTabBarItem0.title = "Test"

            let myTabBarItem1 = (self.tabBar.items?[1])! as UITabBarItem
            myTabBarItem1.image = UIImage(named: "Icon_Outbox")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            myTabBarItem1.selectedImage = UIImage(named: "Icon_Outbox ")?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
            myTabBarItem1.title = "Test"
        }
    }

Now Change the class of UITabBarController in the storyboard to YourTabViewController as shown below. enter image description here

Upvotes: 0

Related Questions