3

I've gone through a tad amount of SO posts about this but none of them clearly state how to get this done in Swift 3.

Simply put, I need to close the current view that is displayed and open a new view controller. Such that the only view controller open would be the 2nd VC

This is the code I tried using, but when I try this the view controller gets closed without the rest of the code getting executed

//Close current VC
self.dismiss(animated: true, completion: nil)

//Open the new VC
let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let secondViewController: SecondViewController = mainStoryboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
self.present(secondViewController, animated: true, completion: nil)

Can someone help me out? I just need a simple transition from 1st VC to 2nd VC and close the 1st VC.

Jay
  • 4,873
  • 7
  • 72
  • 137
  • why are you trying to dismiss current VC? – iAnurag Jan 11 '17 at 08:22
  • you dismiss the controller and present the second controller into dismiss controller. Try to present view controller into rootviewcontroller. – Indrajeet Jan 11 '17 at 08:26
  • 2
    The correct way is delegate to the parentVC to dismiss your current then preset new, if the current is the rootVC then simply set the rootVC again with `UIApplication.shared.delegate.window` – Tj3n Jan 11 '17 at 09:25

3 Answers3

2

The problem is, that "self" can not present the new ViewController since it is the ViewController that is getting dismissed.

You could try calling self.presentingViewController?.present(secondViewController, animated: true, completion: nil)

d.felber
  • 5,288
  • 1
  • 21
  • 36
2

After spend full day i got a solution on Apple' developer website (on this link)

let storyboard = UIStoryboard(name: "Main", bundle: nil)
          let secondVC = storyboard.instantiateViewController(identifier: "second_view_controller")

          secondVC.modalPresentationStyle = .fullScreen
          secondVC.modalTransitionStyle = .crossDissolve

          present(secondVC, animated: true, completion: nil)
Basant
  • 741
  • 7
  • 16
1

If you presented that view controller from some view controller or it is the viewcontroller from navigation or tab viewcontroller, then try to get last visible view controller, code is in this link

Get the current displaying UIViewController on the screen in AppDelegate.m

or else use this code (I have copied from that link)

public extension UIWindow {
    public var visibleViewController: UIViewController? {
        return UIWindow.getVisibleViewControllerFrom(self.rootViewController)
    }

    public static func getVisibleViewControllerFrom(_ vc: UIViewController?) -> UIViewController? {
        if let nc = vc as? UINavigationController {
            return UIWindow.getVisibleViewControllerFrom(nc.visibleViewController)
        } else if let tc = vc as? UITabBarController {
            return UIWindow.getVisibleViewControllerFrom(tc.selectedViewController)
        } else {
            if let pvc = vc?.presentedViewController {
                return UIWindow.getVisibleViewControllerFrom(pvc)
            } else {
                return vc
            }
        }
    }
}

Then present your viewcontroller from that visibleViewController -

//Close current VC

self.dismiss(animated: true, completion: nil)

//Open the new VC

let mainStoryboard: UIStoryboard = UIStoryboard(name:"Main",bundle:Bundle.main)
let secondViewController: SecondViewController = mainStoryboard.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
window?.visibleViewController?.present(secondViewController, animated: true, completion: nil)

If your view controller is the root view controller, then set it to root view controller

if let delegate = UIApplication.shared.delegate as? AppDelegate {
     delegate.window?.rootViewController = secondViewController
 }
Community
  • 1
  • 1
  • this will not answer the question because you will have no visible viewController if you dismiss the first and only one. – zero3nna Jan 11 '17 at 10:13
  • I couldn't find out in his question that he said that the view controller is the only one... can you? – Vinupriya Arivazhagan Jan 11 '17 at 10:16
  • read the headline "Close current ViewController and Open new ViewController" and tell me at which point you are closing any viewController? What you posted will work and is the usual way to go but not for this special case. – zero3nna Jan 11 '17 at 10:23
  • @Zero3nna, I have posted that from where he have to present that view controller, for anyone who can understand this question can understand my answer. – Vinupriya Arivazhagan Jan 11 '17 at 10:26
  • 1
    This is the ticket. Thanks so much. Setting the next view as the root controller is what did ti for me. – Goddard Aug 16 '19 at 13:30