ios - ViewController-Presenter-Interactor 是否应该具有一对一的关系

标签 ios mvp viper-architecture

我正在阅读有关 VIPER 的内容,我的理解是 - 通常, View Controller 与一位演示者相关,一位演示者与一位交互器对话。

但是,如果我们有主详细信息页面或列表详细信息页面怎么办?要显示项目列表,我将让一个 Controller /演示者显示列表,另一个 Controller /演示者显示详细信息。 FetchList 和 FetchDetail 应该属于同一个交互器。

如果这两个演示者与此交互器通信,他们将必须实现 FetchList 和 FetchDetail 方法。这两个方法的实现之一将为空。

最佳答案

您应该有两个独立的 VIPER 模块:MainItems 和DetailedItems。

阅读这篇文章 ( https://www.ckl.io/blog/best-practices-viper-architecture ) 并了解如何使用委托(delegate)在 VIPER 模块之间发送数据。请注意,FetchListFetchDetail 应属于不同交互器:

// 1. Declare which messages can be sent to the delegate

// ProductScreenDelegate.swift
protocol ProductScreenDelegate {
//Add arguments if you need to send some information
    func onProductScreenDismissed()
    func onProductSelected(_ product: Product?)
}

// 2. Call the delegate when you need to send him a message

// ProductPresenter.swift
class ProductPresenter {

    // MARK: Properties
    weak var view: ProductView?
    var router: ProductWireframe?
    var interactor: ProductUseCase?
    var delegate: ProductScreenDelegate?
}

extension ProductPresenter: ProductPresentation {

    //View tells Presenter that view disappeared
    func onViewDidDisappear() {

        //Presenter tells its delegate that the screen was dismissed
        delegate?.onProductScreenDismissed()
    }
}

// 3. Implement the delegate protocol to do something when you receive the message

// ScannerPresenter.swift
class ScannerPresenter: ProductScreenDelegate {

    //Presenter receives the message from the sender
    func onProductScreenDismissed() {

        //Presenter tells view what to do once product screen was dismissed
        view?.startScanning()
    }
    ...
}

// 4. Link the delegate from the Product presenter in order to proper initialize it

// File ScannerRouter.swift
class ProductRouter {

    static func setupModule(delegate: ProductScreenDelegate?) -> ProductViewController {
        ...
        let presenter = ScannerPresenter()

        presenter.view = view
        presenter.interactor = interactor
        presenter.router = router
        presenter.delegate = delegate // Add this line to link the delegate
        ...
        }
}

关于ios - ViewController-Presenter-Interactor 是否应该具有一对一的关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40830159/

相关文章:

ios - 如何将 IBOutlet 值从 View 传递给 VIPER iO 中的交互器?

ios - 如何在 VIPER 结构中使用委托(delegate)传输数据

iOS 毒蛇 : where to put the form validation code?

iphone - AppDelegate 中的 UITabBarDelegate

model-view-controller - 这些图中的箭头(MVC-MVP-MVVM)的图例是什么?

html - iOS WebView控件的HTML中的音频不起作用

wpf - 通过 MVP 中的继承来改变 Presenter

winforms - MVP 主持人之间的沟通?

ios - AVAudioUnitSampler 错误 loadSoundBankAtURL -> 无法找到补丁 0 库 0x0/0

ios - "Linker command failed with exit code 1 (use -v to see invocation)?"如何解决