2

我正在尝试学习 VIPER 架构模型,但我无法弄清楚的一件事是当我执行以下操作时:

  1. 实例化promotionPresenter类
  2. 实例化promotionsViewController
  3. 分配promotionsViewController.presenter =(从步骤1实例化promotionPresenter类)
  4. 尝试从promotionviewController 类中的viewdidload() 函数内部访问实例化的presenter 类。
  5. 主持人是零。为什么主持人是零?我已经实例化了它。
import UIKit

/*
 * The Router responsible for navigation between modules.
 */

class PromotionsWireframe : PromotionsWireframeInput {

    // Reference to the ViewController (weak to avoid retain cycle).
    var promotionsViewController: PromotionsViewController!
    var promotionsPresenter: PromotionsPresenter!
    var rootWireframe: RootWireframe!

    init() {
        let promotionsInteractor = PromotionsInteractor()
        // Presenter is instantiated
        promotionsPresenter = PromotionsPresenter()
        promotionsPresenter.interactor = promotionsInteractor
        promotionsPresenter.wireframe = self
        promotionsInteractor.output = promotionsPresenter
    }

    func presentPromotionsIntefaceFromWindow(_ window: UIWindow) {
        //view controller is instantiated
        promotionsViewController = promotionsViewControllerFromStoryboard()
        //presenter of view controller is assigned to instantiaed class
        promotionsViewController.presenter = promotionsPresenter
        promotionsPresenter.view = promotionsViewController
    }

    private func promotionsViewControllerFromStoryboard() -> PromotionsViewController {
        let storyboard = UIStoryboard(name: "PromotionsStoryboard", bundle: nil )
        let viewController = storyboard.instantiateViewController(withIdentifier: "promotionsViewController") as! PromotionsViewController
        return viewController
    }
}

import UIKit

class PromotionsViewController : UIViewController,    PromotionsViewInterface {

    // Reference to the Presenter's interface.
    var presenter: PromotionsModuleInterface!
    var promotions: [Promotion]!

    /*
     * Once the view is loaded, it sends a command
     * to the presenter asking it to update the UI.
     */

    override func viewDidLoad() {
        super.viewDidLoad()
        // getting error because presenter is unwrapped as nil
        self.presenter.updateView()
    }

    func showPromotionsData(_ promotions: [Promotion]) {

        // need to implement
    }
}

import Foundation

class PromotionsPresenter : PromotionsModuleInterface, PromotionsInteractorOutput {

    // Reference to the View (weak to avoid retain cycle).
    var view: PromotionsViewInterface!

    // Reference to the Interactor's interface.
    var interactor: PromotionsInteractorInput!

    var wireframe: PromotionsWireframe!

    func updateView() {
        self.interactor.fetchLocalPromotions()
    }

    func PromotionsFetched(_promotions: [Promotion]) {
        // need to implement
    }
}
4

1 回答 1

0

我建议你拿这个样板(https://github.com/CheesecakeLabs/Boilerplate_iOS_VIPER)并阅读这篇文章(https://www.ckl.io/blog/best-practices-viper-architecture/)以便学习不仅如何正确初始化您的 VIPER 模块,还包括如何自动创建和初始化 VIPER 文件

于 2017-04-11T02:25:28.587 回答