在viewdidload()

时间:2017-01-15 01:37:59

标签: ios swift xcode null viper-architecture

我正在尝试学习VIPER架构模型,而我无法理解的是,当我执行以下操作时:

  1. 实例化promotionPresenter类
  2. 实例化promotionViewController
  3. 分配promotionsViewController.presenter =(从步骤1实例化的promotionPresenter类)
  4. 尝试从promotionviewController类中的viewdidload()函数内部访问实例化的presenter类。
  5. 主持人是零。 为什么主讲人没有?我已经实例化了它。
  6. 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
        }
    }
    

1 个答案:

答案 0 :(得分:-1)

我建议您使用此样板文件(https://github.com/CheesecakeLabs/Boilerplate_iOS_VIPER)并阅读此帖子(https://www.ckl.io/blog/best-practices-viper-architecture/),以便不仅了解如何正确初始化VIPER模块,还了解如何自动化VIPER文件创建和初始化