I am trying to dismiss a modal and push a new view controller from the navigation controller, but the code bellow dismisses the navigation controller as well, so there is nothing to push to and the window collapses. This code is inside my routing class:
func navigateToVC() {
self.navigationController.presentingViewController.dismiss(animated: false, completion: nil)
self.navigationController.pushViewController(newVC, animated: false)
}
So is there a way to dismiss the presenting view controllers while keeping the navigation controller?
Use this to dismiss presented ViewController,
self.dismiss(animated: true)
Once you dismiss it , that's where you should push ViewController. You can't push after you dismiss your viewController because it's deallocated already.
I recommend creating a delegate and call it from your HomeViewController
Related
I have the next function
func switchRootViewController(rootViewController: UIViewController) {
let window = UIApplication.shared.windows.first!
window.rootViewController = rootViewController
window.makeKeyAndVisible()
}
And I use it in a presented view controller, ex in PresentedViewController.
let navigationViewController = UINavigationController(rootViewController: PresentedViewController())
present(navigationViewController, animated: true, completion: nil)
But when I switch to the needed view controller, my presented view controller doesn't deinitialize. I have to use this way, first dismiss:
self.dismiss(animated: true, completion: {
switchRootViewController(rootViewController: HomeViewController.instantiate())
})
instead of simple
switchRootViewController(rootViewController: HomeViewController.instantiate())
UIViewController objects are retained when presented by another UIViewController. To release your view controllers, they need to be removed from the view hierarchy, which you can accomplish by dismissing them. As far as where to dismiss your view controller, I'm not sure because you haven't given enough information. But you need to dismiss any view controller that you want released from memory.
I have a function inside my app delegate which is supposed to push a View Controller when called from a Siri Shortcut.
func pushNewView() {
if let wind = UIApplication.shared.delegate?.window {
if let rootViewController = wind?.rootViewController {
let viewToPush = TripTableViewController()
if let alreadySomeOneThere = rootViewController.presentedViewController {
alreadySomeOneThere.navigationController?.pushViewController(viewToPush, animated: true)
} else {
rootViewController.navigationController?.pushViewController(viewToPush, animated: true)
}
}
}
}
When I called pushNewView() from my AppDelegate, nothing happens.
If I present the view controller instead of pushing it, that does work but I want the view controller pushed.
I'm not using any storyboards, all my code is programmatic.
Does anyone know how to make this function successfully push the View Controller?
Nothing is happening because neither alreadySomeOneThere nor rootViewController are embedded in a navigation controller.
In order to push another view controller, there needs to be an existing navigation controller in place.
You need to do one of two things. Either redo your setup such that alreadySomeOneThere and rootViewController are embedded in a navigation controller, or you need to present the new view controller from alreadySomeOneThere or rootViewController.
Please note that you can't push a navigation controller. If you choose to put alreadySomeOneThere and rootViewController in a navigation controller, then just push viewToPush. Don't create another navigation controller.
If you instead decide to present the new view controller from alreadySomeOneThere or rootViewController, then depending on the needs of viewToPush, you may or may not need viewToPush embedded its own navigation controller.
My custom AVPlayerViewController won't autorotate if the previous view controller has been instantiated. If I present the AVPlayerViewController, it rotates, and if I present the previous view controller, it rotates. Here's my code for instantiating a view controller :
let vc = self.storyboard!.instantiateViewController(withIdentifier: "MovieVC") as! MovieVC
vc.movie = self.movie
self.navigationController?.pushViewController(vc, animated: true)
In this situation I am creating a copy of the current view controller with different information. If there is no way to fix the autorotating error, is there a different way to present the same view controller?
The problem is that you are not presenting the view controller. You are pushing it. That means that the navigation controller itself is in charge of rotation, so you don't get any special autorotation for this view controller.
The only way to give this view controller its own power over autorotation is to present it:
self.present(vc, animated: true)
I want to move to another view controller without using navigation controller, segue, storyboard and animation
You can use presentViewController like this
let controller = self.storyboard?.instantiateViewControllerWithIdentifier("YOUR_VIEWCONTROLLER_ID")
self.presentViewController(controller!, animated: false, completion: nil)
I've create a Popover segue in my project and inside a button to when it's touched open another ViewController modally. Then inside the another ViewController I've a button to dismiss the actual ViewController and the Popover too with the following code :
var tmpController :UIViewController! = self.presentingViewController;
self.dismissViewControllerAnimated(true, completion: {()->Void in
println("done");
tmpController.dismissViewControllerAnimated(true, completion: nil);
});
My problem is that when the actual ViewController is dismissed the Popover is like in FullScreen, then it resize to its original size and is dismissed after, and the resize process is animated.
What is causing this behaviour ?
How can I avoid this?
I would suggest you to dismiss the popover view controller from the parent view controller using delegation/notification.