【问题标题】:instantiated class is nil when called inside viewdidload()在 viewdidload() 内部调用时实例化的类为 nil
【发布时间】:2017-05-30 03:27:07
【问题描述】:

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

  1. 实例化promotionPresenter类
  2. 实例化promotionsViewController
  3. assign 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
    }
}

【问题讨论】:

    标签: ios swift xcode null viper-architecture


    【解决方案1】:

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

    【讨论】:

    • 这个答案实际上确实将 OP 的问题置于更坚定的关注点分离基础上。相反,这个答案可以通过 answer 的作者精心揭示 Boilerplate_iOS_VIPER 改进的视图、交互者、演示者、实体、路由区域之间的关注点分离来改进。不幸的是,这个答案实际上是一个仅链接的答案(它不需要)而被玷污了。这是不幸的,因为通过查看链接的网页,Marcelo Gracietti 的更大信息(但不幸的是,在 StackOverflow 的答案中省略了)是 OP 需要的大部分真实答案。
    猜你喜欢
    • 2015-12-08
    • 1970-01-01
    • 2011-05-03
    • 1970-01-01
    • 2014-06-18
    • 2012-05-07
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多