On my story board I have a collection view controller embedded in a navigation controller. Now from the cell of the collection view, I programaticly open the second view controller (called TwoPicsViewController), like this:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "TwoPicsViewController") as! TwoPicsViewController
self.present(nextViewController, animated:true, completion:nil)
However when my TwoPicsViewController opens, I don't see the navigation bar at all. On the story board I connected the TwoPicsViewController to the first view controller with a segue show.
Am I missing something else?
Thanks
To push a new UIViewController to the navigation stack you should use:
self.navigationController?.pushViewController(nextViewController, animated: true)
Instead of:
self.present(nextViewController, animated:true, completion:nil)
You are not putting the new vc on the navigation stack, try this:
performSegue(withIdentifier: "Segue Name", sender: nil)
And on Storyboard select the Segue, attributes inspector, identifier = "Segue Name"
Related
I have a view controller (VC3) that I want to push if accessed via one screen (VC1), but present it modally if accessed from a different screen (VC2). The destination view controller (VC3) is on its own storyboard and is embedded in a UINavigationController(which I believe is pretty important here).
In prepare(for segue) on VC1 I have this:
if let nav = segue.destination as? UINavigationController {
let destinationViewController = nav.topViewController as! LiftsViewController
destinationViewController.delegate = self
}
This presents the destination view controller (VC3) modally, which is what I want.
From VC2, the view controller (VC3) will be accessed by tapping on a cell in a table view so I want it pushed. But in prepare(for segue) on VC2 I have the same code as above because, well, I'm segueing to the same view controller which is in the UINavigationController.
I've looked at quite a few threads that address pushing vcs or presenting them modally, but can't find anything that answers how to do both with the same vc.
Thanks.
In this scenario let's not use the segue and push/present your VC's programmatically.
let vc3 = (UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "LiftsViewController") as? LiftsViewController)!
vc3.delegate = self
now push:
self.navigationController?.pushViewController(vc3, animated: true)
or present:
self.presentViewController(vc3, animated: true, completion: nil)
I have navigation controller and tab bar controller. TBC is embeded in NC. When I present VC from another VC, I see only NC and not TBC there. I want them both to be presented. What should I do?
Here is my code:
let mainAreaVC = self.storyboard?.instantiateViewController(withIdentifier: "MainAreaVC") as! MainAreaVC
let mainAreaVCe = UINavigationController(rootViewController: mainAreaVC)
self.present(mainAreaVCe, animated: true, completion: nil)
If you want to show MainAreaVC with both NavigationController and TabBarcontroller then you need to present the UITabBarController instead of MainAreaVC. So in storyboard set the Storyboard Id for your TabBarController something like TabbarVC or what ever you want then use it wit instantiateViewController to get UITabBarController.
let tabbarVC = self.storyboard?.instantiateViewController(withIdentifier: "TabbarVC") as! UITabBarController
self.present(tabbarVC, animated: true, completion: nil)
I have a controller which is in the Main storyboard. When I click on a button, I call the displayBorneDetailsAction() action, it presents a modal view from another storyboard.
I would add a Segue identifier when I present my modal to pass data from my main view controller to my modal view controller (with prepareForSegue), but I don't know how to do it.
I tried to use performSegue(withIdentifier:), but it doesn't present the modal in the same way.
#IBAction func displayBorneDetailsAction(_ sender: Any) {
// open the modal of a borne
let storyboard : UIStoryboard = UIStoryboard(name: "Borne", bundle: nil)
let vc: BorneVC = storyboard.instantiateViewController(withIdentifier: "BorneVC") as! BorneVC
let navigationController = UINavigationController(rootViewController: vc)
navigationController.modalPresentationStyle = UIModalPresentationStyle.overFullScreen
navigationController.edgesForExtendedLayout = []
self.present(navigationController, animated: true, completion: nil)
}
You cannot add an identifier to a programmatical segue, since you are able to access the instance of the controller that is being presented. Simply do anything you want with the controller in the function where you present it.
I've just finished a Swift tutorial which does not use Interface Builder. It is all programmatic. Everything looks great but now I have to segue back to the storyboard, I am lost.
I'd like to segue from the "Login" button designed in the Audible tutorial to the next view controller I created in my storyboard called "DashboardVC".
Here is a link to the tutorial and source code. https://www.letsbuildthatapp.com/course_video?id=382
TIA
As is all from code, you don't have segue's; either you have to push a new controller using pushViewController from NavigationController or instantiate a new ViewController, in this case your 'DashboardVC'.
Like this
let viewController = DashboardVC()
viewController.view.backgroundColor = .blueColor() //example
navigationController?.pushViewController(viewController, animated: true)
Or just presenting the controller using this
let vc = DashboardVC()
present(vc, animated: true, completion: nil)
Using Storyboard, should be like this; the view controller identifier have to be set in the storyboard as well
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier :"DashboardVC") as! DashboardVC
self.present(viewController, animated: true)
The project I am working on currently has multiple storyboards. we are programatically moving from the first storyboard to the next using the following code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DiscoverViewController") as UIViewController
self.present(vc, animated: true, completion: nil)
the storyboard it is moving to has the Storyboard Entry Point pointed at the Navigation Controller of the view controller that is being instantiated and presented above.
The problem is that when the the page is displayed the navigation bar doesn't appear. It appears on the storyboard view, and before we separated the storyboards we weren't having this issue.
Is there an issue with the way I am going from one storyboard to the other? Do I need to present the navigation controller rather than the view controller?
thanks!
This is because you're presenting the view controller, when instead you probably want to push it onto the navigation stack.
This answer shows what you need to do. Here is the code, adapted for your example:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DiscoverViewController") as UIViewController
self.navigationController?.pushViewController(vc, animated: true)