Sagar Pahlajani
Sagar Pahlajani

Reputation: 45

How to add a table view Controller to Tab Bar Controller in Swift

I am using storyboard for all ui layout creation.

I have a table view controller that I have removed on load of application using below code as for normal users one tab bar item need to be hidden.

self.tabBarController?.viewControllers?.remove(at: 4)

But when Admin logs in(I am checking by comparing the email auth) the above removed table view controller should be added, so when I am adding using the below code

self.tabBarController?.viewControllers?.insert(AdminProductUploaderTableVC(), at: 4)

is only adding blank white space. AdminProductUploaderTableVC have navigation controller on Storyboard.

So how to load a existing table view controller for admin?

Thanks

Upvotes: 0

Views: 1968

Answers (1)

Prashant Tukadiya
Prashant Tukadiya

Reputation: 16446

What you are doing wrong ?

self.tabBarController?.viewControllers?.insert(AdminProductUploaderTableVC(), at: 4)

is creating object like this AdminProductUploaderTableVC() instead of loading from storyboard.

First Goto Storyboard and add storyboard identifier to AdminProductUploaderTableVC

Go to AdminProductUploaderTableVC and add Following method

class func viewController() -> AdminProductUploaderTableVC {
    return UIStoryboard.init(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AdminProductUploaderTableVC") as! AdminProductUploaderTableVC
}

Now Replace your line with

self.tabBarController?.viewControllers?.insert(UINavigationController(rootViewController:AdminProductUploaderTableVC.viewController()), at: 4)

Hope it is helpful

Upvotes: 1

Related Questions