UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

The right way to present view controller using coordinators?

Forums > Swift

Hi, guys! Just watched Paul's tutorial about coordinators: https://www.hackingwithswift.com/articles/175/advanced-coordinator-pattern-tutorial-ios

In video code examples he only pushed view controllers, what if we need to present controller modally? What is the right way to do that?

My solution is this, but I don't think that it is the right way:

class MainCoordinator: Coordinator {
    var childCoordinators = [Coordinator]()
    var navigationController: UINavigationController
    weak var presenter: UIViewController?

    init(navigationController: UINavigationController) {
        self.navigationController = navigationController
    }

    convenience init(presenter: UIViewController, navigationController: UINavigationController) {
        self.init(navigationController: navigationController)
        self.presenter = presenter
    }

    func start() {        
        let vc = MainAssembly().createMainModule(coordinator: self)
        presenter?.present(vc, animated: true)
    }
}

2      

@Rool  

You could use the last viewcontroller of the navigationController stack.

  navigationContoller.viewControllers.last?.present(vc, animated: true)

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.