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

Coordinators

Forums > Swift

I've send the following to pauls email. After I found this forum :/ Guess sometimes you cant see the tree in the forest.

I try to learn more about navigation and your articles of the coordinators are great. Though I can’t make it work. You can have a look at the project at https://github.com/GlennDrescher/Disputea

When I create a new Xcode Project I also have Scene Delegate. Maybe that has something to do with it? When I finish the first video about Coordinators I end up with the 2 buttons which do nothing and no navigation bar at the top.

Could you tell me why the buttons are not changing the View?

Another question: What if I want to use Coordinations with optional Navigation controller. For example in the case of Login screen you likely don’t want a back button.

Another thing: Instead of:

func start() {
    let vc = ViewController.instantiate()
    vc.coordinator = self
    navigationController.pushViewController(vc, animated: false)
}

func login() {
    let vc = Login.instantiate()
    vc.coordinator = self
    navigationController.pushViewController(vc, animated: true)
}

func ChatroomList() {
    let vc = ChatroomsList.instantiate()
    vc.coordinator = self
    navigationController.pushViewController(vc, animated: true)
}

I would love to have something less “smelly” like:

func navigate(to page: UIViewController, animated: bool, navigation: bool) {
    let vc = page.instantiate()
    vc.coordinator = self

    if navigation {
          navigationController.pushViewController(vc, animated: animated)
      }
}

navigate(to: Login, animated: true, navigation: false)

<- this could be a cool way to navigate anywhere and choose if you want to push it to the navigation controller or not. But maybe the navigation aspect could better be per Coordinator, like “this section / coordinator has navigation the other don’t"

Any idea why this is not working? I tried with Storyboarded which would be more specific, but then it complaints something about self and it doesn’t work either.

So basically 3 questions:

  1. Why is my project not changing the view and not showing the navigationController bar atop?
  2. How to implement coordinators without navigationbar?
  3. How to navigate anywhere with just one methode instead of duplicated code?

3      

Hi Glenn,

The code you used in the App Delegate should actually go on the Scene Delegate. Paul's video is prior to the change Apple made to introduce scene delegates, that's why his code his in the app delegate.

As for the Login screen, it won't have a "Back" button if it is the first screen the controller presents, so, no change there, I suppose.

As for the "smelly" code fix, it turns out the new code smells more. The first reason is because it increases coupling, since one View Controller has to know the next VC(s). The second is because the goal of the Coordinator is to control (coordinate) the navigation, when a VC knows what the next screen is, it is taking part of that responsibility to itself (which brings us back to the first reason). So, all the VC needs to tell the controller is "I finished my job, here's what I've got in case the next screen needs my data", the coordinator will know what the next screen is and will provide the data, if needed, or even process that data received before passing it on.

I hope that helps.

3      

@GlennDrescher - Had this same problem. Here's my solution. @guseulalio - Thanks for the HUGE clue, which lead me to this source.

Put AppDelegate back the way it was. Add var coordinator: MainCoordinator? property to SceneDelegate and do this to func scene in SceneDelegate (leave everything else alone):

class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    var coordinator: MainCoordinator?
    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {

        guard let windowScene = (scene as? UIWindowScene) else {
            return
        }

        window = UIWindow(frame: UIScreen.main.bounds)
        window?.windowScene = windowScene

        let navController = UINavigationController()
        coordinator = MainCoordinator(navigationController: navController)
        coordinator?.start()

        window?.rootViewController = navController
        window?.makeKeyAndVisible()
    }

4      

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.