1

I have a setup, where I subclassed UINavigationController to add a property holding a reference to another ViewController

class NavControllerWithReference: UINavigationController { 
    public var referenceToOtherVC: OtherVC? 
}

Now when I am in one of the ViewControllers inside this subclassed NavigationController, I want to access the reference property. For a "normal" NavigationController I would typically just access the navigationController property, which would return the navigationController. Obviously, it returns it as UINavigationController, even though it is subclassed, and I cannot do something like:

let navC = navigationController
if navC is [NavControllerWithReference] {  
    print("Yes")
}

This just tells me that:

"Cast from UINavigationController to unrelated type NavControllerWithReference always fails".

How can I access the referenceToOtherVC from the child view controllers?

Pauli
  • 343
  • 1
  • 4
  • 17

1 Answers1

2

You need

guard let nav = self.navigationController as? NavControllerWithReference  else { return }
print(nav.referenceToOtherVC)    
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87