简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM