Is there a trick to push to child view controllers from a parent vc that previously has been presented modally?
Method to present parent that I'm using:
let parentVC = ParentController()
self.present(parentVC, animated: true, completion: nil)
Method to then push to child controllers which doesn't work:
let childVC = childController()
navigationController?.pushViewController(childVC, animated: true)
Is there a trick to push to child view controllers from a parent vc
that previously has been presented modally?
If you present a vc modally and you want this vc to push a child vc, you have to present the vc embedded in a UINavigationController.
let parentVC = ParentController()
self.present(parentVC, animated: true, completion: nil)
to become
let parentVC = ParentController()
let parentNav = UINavigationController(rootViewController: parentVC)
self.present(parentNav, animated: true, completion: nil)
then you can do, in parentVC:
let childVC = childController()
navigationController?.pushViewController(childVC, animated: true)
Related
On dismiss of a .formsheet I need to perform a poptoviewcontroller currently I am presenting the view controller by doing the following:
let controller = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "test") as! TestVC
let navigationController = UINavigationController(rootViewController: controller)
navigationController.modalPresentationStyle = .formSheet
self.present(navigationController, animated: true, completion: nil)
Attempting to dismiss and popToRootViewController underneath is not working
self.navigationController?.dismiss(animated: true, completion: {
print("Dismissing")
self.navigationController?.popToRootViewController(animated: true)
})
The dismiss is working, but it does not popToRootViewController.
When you're accessing the navigationController in your completion, you're accessing the same navigationController you've just dismissed, and not the "root" one.
Here's one of the ways you can access the navigation controller you're interested in and pop to root from it:
let presentingVC = self.presentingViewController
self.navigationController?.dismiss(animated: true, completion: {
print("Dismissing")
presentingVC?.navigationController?.popToRootViewController(animated: true)
})
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 problem... i have two ViewControllers and i use this code to pass from 1st VC to 2nd
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "EVSignInViewController") as! EVSignInViewController
self.present(newViewController, animated: true, completion: nil)
The problem is.. i want to display back button on 2nd VC. I already tried use Editor->Embed in-> Navigation controller on 1st VC. Also i use this
navigationController?.setNavigationBarHidden(false, animated: true)
navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
Presenting a ViewController will present it modally over the top of your current one. It doesn't actually push it onto the navigation controllers navigation stack.
You either need to push the view controller instead
self.navigationController?.pushViewController(newViewController, animated: true)
or wrap it in a navigation controller and then present it
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "EVSignInViewController") as! EVSignInViewController
let navigation = UINavigationController(rootViewController: newViewController)
self.present(navigation, animated: true, completion: nil)
Use this method for present 2nd VC.
self.navigationController?.pushViewController(newViewController, animated: true)
instead of this
self.present(newViewController, animated: true, completion: nil)
You will get back button.
try this
self.navigationController?.navigationBar.isHidden = false
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)