4
votes

In my project i want to present Tabbar on button click, now i have already created tabbar and i give identity name as "tabbar" that i show you in below image

enter image description here

so now i am using below code to call Tab bar controller but i am not getting it.

 let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)

 navigationController?.pushViewController(tabbar, animated: true)

can you guys suggest me what i need to do and which code is useful to me to present Tabbar controller.

For more specification : i added below image in which there is blue button in one viewController i want to present tab bar controller on click of that blue button

enter image description here

Thank you.

5
Emed the tab bar in a navigation controllerSh_Khan
i already create tab with it but now i want to call that tab bar controller on button clickBhaumik Joshi
You navigation controller or storyboard object is nilSh_Khan
@BhaumikJoshi i am not sure, but i don't think so you can push a tab bar in navigation stack, instead on button click make it the root controller.Tushar Sharma
@TusharSharma i don't have more idea that u told can you give me detailed idea.Bhaumik Joshi

5 Answers

3
votes

Try this and see:
Using Storyboard Segue: Connect your segue as present modally with your action button.

enter image description here

Programatically: Use self of view controller (UIViewController) and present it modally, like this.

if let tabbar = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController) {
    self.present(tabbar, animated: true, completion: nil)
}

Here is result:

enter image description here

1
votes

Use this, you dont have the Navigation controller over there, thats why it won't push that way, instead, you need to use following code:

self.present(tabbar, animated: true, completion: nil)
0
votes

You should be aware that Apple's HIG (Human Interface Guidelines) say that if you have a tabbed application that should be the root-level navigation for the entire app. you're not supposed to do what you are trying to do from a human interface perspective.

That said, it should be technically possible.

My guess is that you don't have a navigation controller.

Use the debugger to check the value of self.navigationController, or add a print statement:

 let tabbar: UITabBarController? = (storyboard.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)
 print("navigationController = \(navigationController)")
 print("tabbar = \(tabbar)")

 navigationController?.pushViewController(tabbar, animated: true)

If either navigationController or tabbar displays as nil, that's your problem.

0
votes

Select the viewController from which the button click triggered and Select Editor form xcode menu->Embed In->NavigationController.

then write this in button action

   let tabbar: UITabBarController? = (self.storyboard?.instantiateViewController(withIdentifier: "tabbar") as? UITabBarController)

    self.navigationController?.pushViewController(tabbar!, animated: true)
0
votes

According to your images I think that your tabBarController has no back button. Means user cannot go back.

If that is the case you can do this.

    let tabBar = self.storyboard?.instantiateViewController(withIdentifier: "tabBar")
   let appDelegate = UIApplication.shared.delegate as! AppDelegate
    appDelegate.window?.rootViewController = tabBar