TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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 Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your spot now

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.