Search code examples
iosswiftuiviewcontrollerpopupsegue

Segue from popup ViewController Swift


I have app with 7 screens(VC) which are embedded in navigation controller.

And I have one separate VC that is not embedded. This VC is acting like custom popup class PopupView: UIViewController {} and get's called by pressing button from every screen that I have in my app using custom segue (setup as custom segue in storyboard):

open class MIBlurPopupSegue: UIStoryboardSegue {

open override func perform() {
    MIBlurPopup.show(destination, on: source)
} }

In this popup there's a button that should open another VC (VC always the same) that is embedded in navigation stack.

What I'm trying to achieve is to actually open that VC that is inside navigation stack by pressing button in Popup VC and then get back to the screen where Popup was called.

So, user journey will look like - Opened VC1(2,3,5,6,7) -> Called popup VC -> Pressed button -> opened VC4 -> pressed navigation back button -> returned to VC1.

I what I have right now:

  • Connected in storyboard all 6 screens to VC4, with segues ids

  • Tried performSegue(withIdentifier: "toVC4"), present(vc, animated: true, completion: nil)

  • let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil) let controller = storyboard.instantiateViewController(withIdentifier: "VC4") self.present(controller, animated: true, completion: nil)

  • Using protocols to call func in VC1 but failed.

I'm definitely missing something and would be very thankful if someone could provide code sample to solve this issue.


Solution

  • You can try this inside the popup button's action

    self.dismiss(animated:true) {
       if let nav = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
          let vc1 = storyboard!.instantiateViewController(withIdentifier: "MenuId")
          let finalVC = storyboard!.instantiateViewController(withIdentifier: "finalId")
          nav.setViewControllers([vc1,vc4],animated:true) // set it to false if you want 
       }
    }