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

Coordinator Pattern - Which way is right?

Forums > Swift

Hey guys. I am trying to understand the way that Coordinator works. I am cathing myself in building my own the way I shall not (created a function that passes a ViewController which, as I understand, defeats the purpose of a Coordinator.

Let us say I have 3 Diffrent ViewControllers that may be initialised from HomeVC depending on a Button. My way of thinking tells me to create something like this:

class MainCoordinator: Coordinator {

    var navigationController: UINavigationController?
    // MARK: - Coordinator: Run the App

    func pushVCWithUser(viewController: UIViewController & Coordinating, isNavigationHidden: Bool) {
        var pushedVC: UIViewController & Coordinating = viewController
        pushedVC.coordinator = self
        navigationController?.setNavigationBarHidden(isNavigationHidden, animated: true)
        navigationController?.pushViewController(pushedVC, animated: true)
    }
}

and just to initialise it in my VC. But I it's not the way it supposed to be done I assume. Or is it actually?

Another way:

I have seen some tutorial when a guy used Enum for cases and created seperated function for each case. But then I don't know how would I pass some parameter while initialising another ViewController.

Or shall I rather create a function like the one above but for each individual View Controller and pass the parameter (lets say that VCs are initialised with the number: Int) in a function signature?

Many thanks for help!

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free 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.