Search code examples
iosswiftuinavigationcontrollersegue

Can you push to a viewController and be a part of that controllers navigation?


Lets say I have the following storyboard structure

navigation controller -> class A -> class B

And seperate from the navigation controller I have class C.

Now I would like to navigate from class C´ to class Band end up inside thenavigation controllerof whichclass Bbelongs. So if I press back, I end up inclass A`.

I tried by creating a seperate class, subclassed from UINavigationController

class MyNavigation: UINavigationController {}

Then in the storyboard I set the navigationController to this class.

In class C I tried the following code:

let navigation = UINavigationController() as? Myavigation

let storyboard = UIStoryboard(name: "MyStoryboard", bundle: nil)

guard let viewController = storyboard.instantiateViewController(withIdentifier: "MyId") as? FirmwareViewController else { return }
           
navigation?.pushViewController(viewController, animated: true)

But this did nothing. And yes, it passed the guard. So is it possible to achieve what I'm trying to do?


Solution

  • I think you can use following -

    let cNavCtrl = UINavigationController(rootViewController: c)
    cNavCtrl.modalPresentationStyle = .fullScreen
    b.navigationController?.present(cNavCtrl, animated: false, completion: nil)
    

    Then from inside c, you can add a backButton/cross that'd call following -

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

    This way - your c is never a part of b.navigationController and tapping the back from c takes you back to b.