简体   繁体   English

Swift:如何使用 Xcoordinator 从子协调器设置根视图 controller?

[英]Swift: How to use Xcoordinator to set a root view controller from child coordinator?

I'm trying to understand how to use XCoordinator to set a root view controller from a child coordinator.我试图了解如何使用 XCoordinator 从子协调员那里设置根视图 controller。

I have an app delegate that runs the app coordinator.我有一个运行应用程序协调器的应用程序委托。

The pattern I have is:我的模式是:

AppDelegate - App Coordinator --- Main Menu Coordinator --- Game Coorinator AppDelegate - 应用协调器 --- 主菜单协调器 --- 游戏协调器

(see image) (见图)

协调器图

The app coordinator should not know about specific view controllers directly because I believe the view controllers should managed by their own respective coordinator.应用程序协调器不应该直接了解特定的视图控制器,因为我认为视图控制器应该由它们各自的协调器管理。

Thus, the app coordinator should manage the flow between the coordinators and set a root, initial coordinator.因此,应用程序协调器应该管理协调器之间的流程并设置一个根初始协调器。

Sadly, the screen is blank, and keeps complaining that I can't as "Pushing a navigation controller is not supported".可悲的是,屏幕是空白的,并且一直抱怨我不能“不支持推送导航 controller”。

But if I directly instantiate the view controller inside my appcoordinator its fine and I don't know why.但是,如果我直接在我的 appcoordinator 中实例化视图 controller 就很好,我不知道为什么。

// AppCoordinator
enum AppRoute: Route {
    case mainmenu
    case game
}

class AppCoordinator: NavigationCoordinator<AppRoute> {

    init() {
        super.init(initialRoute: .mainmenu)
    }

    // MARK: Overrides
    override func prepareTransition(for route: AppRoute) -> NavigationTransition {
        let router = MainMenuCoordinator().strongRouter
        return .push(router)
    }
}

This should launch the MainMenuCoordinator's initial view controller这应该会启动 MainMenuCoordinator 的初始视图 controller

enum MainMenuRoute: Route {
    case mainmenu
}

// MainMenuCoordinator
class MainMenuCoordinator: NavigationCoordinator<MainMenuRoute> {

    init() {
        super.init(initialRoute: .mainmenu)
    }


    override func prepareTransition(for route: MainMenuRoute) -> NavigationTransition {
        print ("route: \(route as Any)")

            let vc = MainMenuViewController.instantiate(.main)
            return .push(vc)
     }
}

But this returns a blank screen and it complaining that push is not supported.但这会返回一个空白屏幕,并抱怨不支持推送。

But, if I move this code:但是,如果我移动这段代码:

let vc = MainMenuViewController.instantiate(.main)
return .push(vc)

to the AppCoordinator到 AppCoordinator

like this:像这样:

// MARK: AppCoordinator Overrides
override func prepareTransition(for route: AppRoute) -> NavigationTransition {
    let vc = MainMenuViewController.instantiate(.main)
    return .push(vc)
}

Its fine.没关系。 A screen is presented -- but the main menu coordinator is no longer being "followed"出现了一个屏幕——但主菜单协调器不再被“关注”

I'm wondering -- how do you make it so that the app coordinator just delegates responsibility to its child coordinator?我想知道 - 你是如何做到的,以便应用程序协调员将责任委托给它的子协调员?

I appreciate any assistance you can give.感谢您提供的任何帮助。 Thanks谢谢


// Edit : I have tried another attempt, following an answer provided, of passing a reference of the parent coordinator to child coordinators. // 编辑:根据提供的答案,我尝试了另一次尝试,将父协调器的引用传递给子协调器。

I want the mainmenucoordinator to use its own collection of routes;我希望mainmenucoordinator使用它自己的路由集合; I don't want to just rely upon on any routes defined in the AppRoutes .我不想仅仅依赖AppRoutes中定义的任何路由。

So my app coordinator has these routes:所以我的应用协调员有这些路线:

enum AppRoute: Route {
    case mainmenu
    case game
}

class AppCoordinator: NavigationCoordinator<AppRoute> {

init() {
    let defaultRoute: AppRoute = .mainmenu
    super.init(initialRoute: defaultRoute)
    self.rootViewController.view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}

}

My main coordinator has these routes:我的主要协调员有以下路线:

enum MainMenuRoute: Route {
    case mainmenu
    case selectPlayer
    case playgame
}

class MainMenuCoordinator: NavigationCoordinator<MainMenuRoute> {

    // For my child coordinators, keep a strong reference to parent
    private var router: StrongRouter<AppRoute>?
/// ...
}

When I come to init it, I set the parent.当我开始初始化它时,我设置了父级。

// MainMenuCoordinator: Init
  convenience init(router: StrongRouter<AppRoute>) {
        self.init(initialRoute: .mainmenu)
    }

But now it won't use any routes that I've defined for the MainMenuCoordinator ;但现在它不会使用我为MainMenuCoordinator定义的任何路由; instead, it uses AppRoute相反,它使用AppRoute

Example:例子:

Trying to set the router in MainMenuCoordinator.prepareTransition尝试在MainMenuCoordinator.prepareTransition中设置路由器

override func prepareTransition(for route: MainMenuRoute) -> NavigationTransition {
        print ("route: \(route as Any)")

        switch route {
        case .mainmenu:
            let vc = MainMenuViewController.instantiate(.main)
            vc.router = strongRouter
            return .push(vc)
        // ....
      }
}

The main menu view controller holds a reference to:主菜单视图 controller 包含对以下内容的引用:

class MainMenuViewController: UIViewController, Storyboarded {

    // MARK: - Stored properties
    var router: UnownedRouter<MainMenuRoute>!
}

With these edits, the root view controller is now white;通过这些编辑,根视图 controller 现在是白色的; but the logger still complains I cannot push view controllers.但记录器仍然抱怨我无法推送视图控制器。

I am doing smth like this:我正在这样做:

class MainCoordinator: NavigationCoordinator<ApplicationRoute> {

    convenience init() {
        var defaultRoute: ApplicationRoute = .showAuthentification
        if ApplicationSettings.shared.isLoggedIn {
            defaultRoute = .showApplicationRoot
        }

        self.init(defaultRoute: defaultRoute)
        self.rootViewController.view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    }

    override func prepareTransition(for route: ApplicationRoute) -> NavigationTransition {
        switch route {
            case .showAuthentification:
                return .multiple(.dismissToRoot(animation: .fadeInstant),
                                 .presentFullScreen(AuthentificationCoordinator(router: strongRouter),
                                                    animation: .fadeInstant))

            case .showApplicationRoot:
                return .multiple(.dismissToRoot(animation: .fadeInstant),
                                 .presentFullScreen(TabControllerCoordinator(router: strongRouter),
                                                    animation: .fadeInstant))

        }
    }
}

and in a one child coordinator:在独生子女协调员中:

class AuthentificationCoordinator: NavigationCoordinator<AuthentificationRoute> {
    // For my child coordinators I keep a strong reference to the parent, so I can use it later
    private var router: StrongRouter<SMCApplicationRoute>?

    convenience init(router: StrongRouter<SMCApplicationRoute>) {
        self.init(defaultRoute: .initial)
        self.router = router
    }

    override func prepareTransition(for route: AuthentificationRoute) -> NavigationTransition {
        switch route {
        case .initial:
            let initialVC = LandingPageViewController.loadFromXIB(type: LandingPageViewController.self)
            initialVC.router = strongRouter
            return .push(initialVC, animation: .fade)

        case .showLogin:
            let loginVC = LoginViewController.loadFromXIB(type: LoginViewController.self)
            loginVC.router = strongRouter
            return .push(loginVC, animation: .fade)

        case .showHome:
            // Here I trigger the other route from MainCoordinator. If I log in, the AuthentificationCoordinator will get destroyed, with all the child coordinators with root controllers
            return .trigger(SMCApplicationRoute.showApplicationRoot, on: router!)
        }
    }
}

I think there are better solutions, but I found this to be efective.我认为有更好的解决方案,但我发现这是有效的。 If the code is not self explaining, just let me know;)如果代码不能自我解释,请告诉我;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Swift 4-如何从当前视图控制器设置和显示新的根视图控制器 - Swift 4 - How to set and show a new root view controller from current view controller 如何快速从父视图控制器访问子视图控制器? - How to access the child view controller from parent view controller in swift? Swift /如何设置(和加载)新的根视图控制器 - Swift / How to set (and load) a new root view controller 如何设置根视图控制器? - How to set root view controller? 根视图控制器Swift - Root View Controller Swift 如何快速将 ManagedObjectContext 传递给根视图控制器 - How to pass the ManagedObjectContext to the root view controller in swift 如何从AppDelegate.swift访问不是根视图控制器的视图控制器? - How to access a view controller that is not the root view controller from AppDelegate.swift? 如何快速显示来自子视图控制器的解析登录 - How to redisplay parse login from child view controller in swift 从第四个视图控制器转到根视图控制器swift - Go from third of forth view controller to root view controller swift 如何从根视图控制器到导航堆栈中的最后一个视图控制器快速执行segue? - how to perform segue from root view controller to the last view controller in the navigation stack in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM