Taras Tomchuk
Taras Tomchuk

Reputation: 331

Navigation Controlled embedded in Container View custom segue

I have UIContainerView inside which I have UINavigationController and other VC embedded in UINavigationController (7 of them).

So, it looks like this: Screenshot of app

On that 7 screens inside UINavigationController I'm calling a popup screen which then should redirect to other screen, from which user can perform back segue to the VC popup was called from.

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

Here's my code:

@IBAction func didPressAuthors(_ sender: UIButton) {
    self.dismiss(animated: true) {

        if let navigation = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {
            let vc1 = self.storyboard!.instantiateViewController(withIdentifier: "FirstVC")
            let vc2 = self.storyboard!.instantiateViewController(withIdentifier: "SecondVC")
            let vc3 = self.storyboard!.instantiateViewController(withIdentifier: "ThirdVC")
            let vc4 = self.storyboard!.instantiateViewController(withIdentifier: "FourthVC")
            let vc5 = self.storyboard!.instantiateViewController(withIdentifier: "FifthVC")
            let finalVC = self.storyboard!.instantiateViewController(withIdentifier: "Authors")
            let userJourney = self.defaults.array(forKey: DefaultKeys.screenID.rawValue)

            for vcName in userJourney! {
                switch String(describing: vcName) {
                case "FirstVC":
                    self.journeyVC.append(vc1)
                case "SecondVC":
                    self.journeyVC.append(vc2)
                case "ThirdVC":
                    self.journeyVC.append(vc3)
                case "FourthVC":
                    self.journeyVC.append(vc4)
                case "FifthVC":
                    self.journeyVC.append(vc5)
                default:
                    self.journeyVC.append(finalVC)
                }
            }
            self.journeyVC.append(finalVC)
            navigation.setViewControllers(self.journeyVC, animated: true)
        }
    }
}

Problem:

When I didn't have UINavigationController inside UIContainerView this code worked just fine, but after I had added Container View - it stopped. Dismiss func is called, but nothing happens. What I'm missing? Should I change how VC is presented?

Would be grateful if anyone could help me fix this issue. Many thanks and have a nice weekend!

Upvotes: 0

Views: 31

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Your current root isn't the navigation here

if let navigation = UIApplication.shared.keyWindow?.rootViewController as? UINavigationController {

it's a usual VC that the navigation is inserted as a child so try

if let root = UIApplication.shared.keyWindow?.rootViewController as? RootVC {
let nav = root.children![0] as! UINavigationController  

Upvotes: 2

Related Questions