0

As shown in this picture, I have root navigation controller which has its root viewcontroller as VC1.

In VC2 i present Modally with the option: Over Current Context in order to get the Blur effect.

In VC4, User has the option to navigation back to the root viewcontorller VC1. which is shown in the large Red Arrow.

explain

I tried 2 ways:

First:

In VC4:

func popToRoot(){
    self.dismiss()
    self.navigationController?.popToRootViewController(animated: animated)
}

The Problem: This code navigation back to VC2.

Second:

I tried to have a public static boolean which i set it true if user wants to go back to VC1

VC1:

class VC1: UIViewController {
    public static var shouldGoBackToMainRoot = false;

    override func viewDidAppear(_ animated: Bool) {
        VC1.shouldGoBackToMainRoot = false
    }    
}

VC2:

class VC2: UIViewController {
    override func viewDidAppear(_ animated: Bool) {
        if VC1.shouldGoBackToMainRoot {
            self.dismiss()
            self.navigationController?.popToRootViewController(animated: animated)
            return
        }
    }    
}

VC4:

class VC4: UIViewController {
    // .. other code ...

    func onGoBackToRootClicked() {
        VC1.shouldGoBackToMainRoot = true
        self.dismiss()
        self.navigationController?.popToRootViewController(animated: animated)
    }
}

The Problem: In VC2: viewDidAppear and viewWillAppear will not get called since i presented VC3 with Over Current Context.

NOTE: Removing Over Current Context will let VC2's viewDidAppear function to get called, but i will lose the Blur effect. which is not what i want.

Question: How to navigate from VC4 to VC1 without losing the Blur effect ?

Community
  • 1
  • 1
MBH
  • 16,271
  • 19
  • 99
  • 149
  • check this for unWind segue https://stackoverflow.com/questions/12561735/what-are-unwind-segues-for-and-how-do-you-use-them – kirti Chavda Jun 20 '17 at 11:09

2 Answers2

1

Do not present VC3 on VC2 instead of this just present it overCurrentContext on self.window.rootViewController in AppDelegate And make a strong reference of your firstNavigationController in AppDelegate So in your VC4 when you want to go back to VC1 all you have to do is...

let delegate = UIApplication.shared.delegate as? AppDelegate
delegate.rootNavigationController.popToRootViewController(animated: true)

This is remove a viewController from your bottomNavigationStack that is under the VC4 and will not remove the VC4 from the top.

Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52
1

Write a delegate from VC4 to VC2. In VC2 dismiss the presented navigation controller. On completion of dismiss operation, then use popToRootViewController to navigate back to VC1.