首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >没有链接的viper模块

没有链接的viper模块
EN

Stack Overflow用户
提问于 2022-10-20 12:22:35
回答 1查看 18关注 0票数 0

我下载了viper模块的模板,它在视图中没有链接,我如何在这里使用导航?

现场代表

代码语言:javascript
运行
复制
let startModule = CardsCollectionRouter.createModule()
        let navigationController = UINavigationController(rootViewController: startModule)
        window?.rootViewController = navigationController
        window?.makeKeyAndVisible()

路由器

代码语言:javascript
运行
复制
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一样,我将在ViewController上使用这个方法

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-10-20 12:37:20

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

代码语言:javascript
运行
复制
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)
    }
    
    
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74139824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档