how to manage view hierarchy when opening a viewcontroller from push notification

206 Views Asked by At

I have managed to open a view controller from app delegate when clicked on push notification from viewcontroller with didReceive method. My issue is managing the hierarchy . Consider I have View A, B and C such that I will get to see View C in these steps: View A -> View B -> View C. But when I click on push notification it takes me to View C so when i click on back button of View C it should have View B and View A in the same stack order. Is it possible to do so?

Edit: This is my code if it helps

I want BaseVc > secondBaseVc > LogbookVc > DetailVc

        if let info = userInfo as? [String:Any]{
        let id = info["id"] as? String ?? ""


        let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        let BaseVc = storyBoard.instantiateViewController(withIdentifier: "BaseViewController") as! BaseViewController
        let secondStoryboard : UIStoryboard = UIStoryboard(name: "Second Storyboard", bundle: nil)

        let secondBaseVc : BaseController = secondStoryboard.instantiateViewController(withIdentifier: "secondBaseVc") as! secondBaseVc
        let LogbookVc : LogbookVc = secondStoryboard.instantiateViewController(withIdentifier: "LogbookVc") as! LogbookVc
        let DetailVc: DetailVc = secondStoryboard.instantiateViewController(withIdentifier: "DetailVc") as! DetailVc

    }

How should i connect them to show DetailVc but get them in order so as when i click back button i should have Logbook < secondBaseVc < Base Vc

1

There are 1 best solutions below

2
On

Not actually, if you are not having those viewControllers A and B in stack, then you can not have something to pop to previous viewController.

In my opinion, what you can do is: You have to handle this by your self, like, when notification received. You have to first push A, then B and then C, by putting animation: false, so it will not produce the effect of transition.

.navigationController?.pushViewController(objA, animated: false) .navigationController?.pushViewController(objB, animated: false) .navigationController?.pushViewController(objC, animated: false)

EDIT

        let navigationController = UINavigationController(rootViewController: baseVc)
        navigationController.pushViewController(logbookVc, animated: false)
        navigationController.pushViewController(detailVc, animated: false)

        self.window?.rootViewController = navigationController
        self.window?.makeKeyAndVisible()

IMPORTANT NOTE:

Create your object name with camelCase for proper readability. e.g let BaseVc should be let baseVc and same for the logbookVc and detailVc

Try and share the results.

Hope it helps.