视图中没有链接的毒蛇模块

s71maibg  于 2022-10-23  发布在  Swift
关注(0)|答案(1)|浏览(353)

我下载了毒蛇模块的模板,但它在视图上没有链接,我如何在这里使用导航?
场景代理

let startModule = CardsCollectionRouter.createModule()
        let navigationController = UINavigationController(rootViewController: startModule)
        window?.rootViewController = navigationController
        window?.makeKeyAndVisible()

路由器

class CardsCollectionRouter: PresenterToRouterCardsCollectionProtocol {

    // MARK: Static methods
    static func createModule() -> UIViewController {

        let viewController = CardsCollectionViewController()
        let presenter: ViewToPresenterCardsCollectionProtocol & InteractorToPresenterCardsCollectionProtocol = CardsCollectionPresenter()
        let networkService = NetworkService()

        viewController.presenter = presenter
        viewController.presenter?.router = CardsCollectionRouter()
        viewController.presenter?.view = viewController
        viewController.presenter?.interactor = CardsCollectionInteractor(networkService: networkService)
        viewController.presenter?.interactor?.presenter = presenter

        return viewController
    }

    func showDescription(description: CharacterModel?) {
        let descriptionVC = DescripModuleRouter.createModule(description: description)
        //How to go on next screen??
        let viewController = CardsCollectionViewController()
        viewController.navigationController?.pushViewController(descriptionVC, animated: true)
    }

}

也许我有一种简单的方法可以获得到该视图的链接?
就像路由器上的view.goToAnotherScreen,我将在我的视图控制器上使用此方法

sdnqo3pr

sdnqo3pr1#

在使用此模式时,您不应该在路由器外部创建导航方法,因为导航是路由器的责任。您可以创建router.goToAnotherScreen并从您的视图控制器调用它。在创建路由器时,您应该将视图控制器传递给路由器,并且在您的路由器内部应该调用viewController.navigationController?.push...

class CardsCollectionRouter: PresenterToRouterCardsCollectionProtocol {

    // MARK: Dependencies
    private weak var viewController: UIViewController!

    // MARK: Initialization
    init(viewController: UIViewController) {
        self.viewController = viewController
    }

    // MARK: Static methods
    static func createModule() -> UIViewController {
        let viewController = CardsCollectionViewController()
        let presenter = CardsCollectionPresenter()
        let networkService = NetworkService()

        viewController.presenter = presenter
        viewController.presenter?.router = CardsCollectionRouter(viewController: viewController)
        viewController.presenter?.view = viewController
        viewController.presenter?.interactor = CardsCollectionInteractor(networkService: networkService)
        viewController.presenter?.interactor?.presenter = presenter

        return viewController
    }

    func showDescription(description: CharacterModel?) {
        let descriptionVC = DescripModuleRouter.createModule(description: description)
        //How to go on next screen: Use your VC instance, which is the one your VIPER scene is controlling
        self.viewController.navigationController?.pushViewController(descriptionVC, animated: true)
    }

}

相关问题