NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

SOLVED: problem with the NavigationController

Forums > Swift

Hey Guys.

I have a VC.

Inside that VC there is a custom UI View.

In that custom UI View there is a Collection View.

When I select a cell form that Collection View I want to present modally some other VC.

The problem is I don't have access to Navigation Controller that hold that first VC. When I do try to create a new Navigation Controller in didSelectItemAt, I get the error when I tap the cell that Navigation Controller is trying to present a VC that does not belong to its hierarchy.

How can I resolve that issue?

Many thanks.



so either you need to have hold on the container view controller, maybe setting it to your custom UI view,

or you can use the delegate pattern and the opening of the new view controller will be done in the class that implements delegate for this custom view,

or you can use the UIApplication.shared property and with some logic get the current top-most view controller and use it to present new one.

I think you can easily google examples about getting the top VC. The implementation might be a bit different if you are using something like TabBarController for you rootViewController or something that provides the sliding menu..


Hacking with Swift is sponsored by Play

SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!

Click to learn more about Play!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.