ajrlewis
ajrlewis

Reputation: 3058

iOS (Swift): Presenting View Controller Embedded in Navigation Controller

I have a UIViewController (AVC) that is embedded in a UINavigationController. AVC (present modally) segues to another UIViewController (BVC). Inside BVC, the variable self.presentingViewController is of type an optional NavigationController rather than a AVC as I would have expected.

I have to downcast the first childViewControllers as an AVC as follows:

let pvc = self.presentingViewController
if let avc = pvc?.childViewControllers.first as? AVC {
    // ...
}

Why is self.presentingViewController not as I expected it to, i.e. an AVC?

Many thanks.

Upvotes: 3

Views: 1344

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100543

To access it

if let pvc = self.presentingViewController as? UINavigationController {
    if let avc = pvc.viewControllers.first as? AVC {
       // ...
    }
}

//

From Docs

When you present a view controller modally (either explicitly or implicitly) using the present(_:animated:completion:) method, the view controller that was presented has this property set to the view controller that presented it. If the view controller was not presented modally, but one of its ancestors was, this property contains the view controller that presented the ancestor. If neither the current view controller or any of its ancestors were presented modally, the value in this property is nil.

Upvotes: 2

Related Questions