Push View Controller from Tab Bar Controller - swift

My tab bar controller is not embedded in a navigation controller.
On index 1 (which is embedded in a navigation controller) I have an alert that should show a view controller inside index 2 (also embedded in its own navigation controller).
I would like to jump to the view controller which is inside index 2 (called subVC). The following code does not work. navController is coming up nil.
let subVC = self.storyboard?.instantiateViewController(withIdentifier: Constants.Storyboard.subscriptionVC) as! SubscriptionViewController
self.selectedIndex = 2
let navController = self.tabBarController?.selectedViewController?.navigationController
navController?.pushViewController(subVC, animated: true)

Related

How to embed a view controller into a navigation view controller programmatically

I am trying to embed my view controller into a navigation view controller so i get the navigation bar and all the other stuff like back buttons. I want to do it programmatically.
It is done like this:
// example ViewController
let myVC = UIViewController()
// create the NavigationController with my VC as root
let navCon = UINavigationController(rootViewController: myVC)

How to display the tabs when click the back button from hided tab view controller

I created a tab bar controller and from one tab item I gave segue to the navigation view controller. And I create a some view controllers attached to navigation controller. So in one view controller I don't need a tabs so in that controller I wrote to hide the tab bar controller that is self.tabBarController?.tabBar.isHidden = true.
When I click the back button of navigation controller from hided tab view controller to previous controller, it doesn't show the tab bar items in previous controllers. But I needed tabs in all view controller except in one view controller. Why does it not show the tabs?
This is my story board :
You can try this in the VC that's before the one you hide the tab in
override func viewWillAppear(_ animated:Bool) {
super.viewWillAppear(animated)
self.tabBarController?.tabBar.isHidden = false
}
You can use hidesBottomBarWhenPushedin the view controller which you don't need tabs. Fits your situation.
let controller = ViewControllerTwo()
controller.hidesBottomBarWhenPushed = true
navigationController?.pushViewController(controller, animated: true)
A little more explanation:
self.tabBarController?.tabBar.isHidden = true globally changed the self.tabBarController's property hideTabBar across its children controllers stack.

Go back to tab bar controller with index 2

this is my procedural scheme
i have a tab controllore with 3 items and into 1 items i have a navigation controller than into this i have a popup
i need to go back from my popup to my tab bar controller with index 2
this is my code but doesn't work.
let TabViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabViewController") as! TabViewController
self.present(TabViewController, animated: true)
I have to be able to go back from my popup, to my tab bar controller directly to the page with item 2
You can try
self.dismiss(animated:true) {
let TabViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabViewController") as! TabViewController
TabViewController.selectedIndex = 2
UIApplication.shared.keyWindow?.rootViewController = TabViewController
}

navigationController is nil in swift

I have two storyboard (Main and SB2)
I have one view controller in storyboard in Main and one in SB2
I perform
let SB2 = UIStoryboard(name: "SB2", bundle:nil)
let vc : UIViewController = self.SB2.instantiateViewControllerWithIdentifier("VC2")
self.showViewController(vc, sender: self)
After the second viewcontroller loads and this command is run, it prints nil:
print(self.navigationController) // prints nil
In SB2 (second storyboard) I clicked on the viewcontroller (VC2) and then clicked on Editor > Embed In > Navigation Controller. This placed a navigation controller with the storyboard and the root view controller is VC2. I triple checked this. The first sign of it being connected was the gray navigation bar on top. The second being that there is segue connecting the navigation controller to VC2 and the last place I could have checked is in the navigation controller utilities.
I am thinking that maybe I shouldn't transition from VC1 to VC2 directly but rather VC1 to NavigationController however I don't know how to do this or if its even possible.
I don't know when sb2 prints nil when in face a navigation controller is connected to it. Any help is appreciated.
You need to push the view controller (VC2) on to the navigation controller.
let SB2 = UIStoryboard(name: "SB2", bundle:nil)v
let nvc = SB2.instantiateViewControllerWithIdentifier("NVC") as! UINavigationController
let vc : UIViewController = self.SB2.instantiateViewControllerWithIdentifier("VC2")
nvc.pushViewController(vc, animated: true)
Then in your view controller try calling
self.navigationController
In your storyboard,
select the initial ViewController. With this view controller selected, choose the menu item
Editor -> Embed In -> Navigation Controller

How to present a view controller inside a navigation controller SWIFT

I have VC1 which is a basic view controller, I then want to present VC2 which is inside a navigation controller. Whenever I present it, it doesn't display the navigation controller. I want this all done pragmatically. The code I have been using to present VC2 is:
func matchesPressed(sender: UIButton!) {
let matchesTVC: Matches = self.storyboard?.instantiateViewControllerWithIdentifier("Matches") as! Matches
self.presentViewController(matchesTVC, animated: true, completion: nil)
}
How do I present the navigation controller it is inside as well?
Is the navigation controller in the storyboard? If so, give it a storyboard ID and replace your matchesTVC stuff with the navigation controller.
If matches is a standalone view controller in the storyboard, you can do it in code like this:
let matchesTVC: Matches = self.storyboard?.instantiateViewControllerWithIdentifier("Matches") as! Matches
let navContr = UINavigationController(rootViewController:matchesTVC)
self.presentViewController(navContr, animated: true, completion: nil)
Instantiate the navigation controller that contains your view controller and present the navigation controller.