料器功能是从来不看的迅速3(presenter function is never read on

2019-11-05 03:11发布

我打电话给我的演讲functon是在我的演讲正在实施的协议中列出,但每当我把它它永远不会走了进去,我怎么用来初始化我的演讲,所以我可以调用接口funcion?

这里是我的视图控制器:

import UIKit

class FirstScreenViewController: UIViewController, MainViewProtocol {

    var myPresenter: MainPresenterProtocol?

    func outputPresenterFunction() {
       print (myPresenter?.presenterProtocolFuncTwo(numOne: 2, numTwo: 4, sucssesMessage: "Made it ?", failMessage: "failed :(" ) ?? "Default landed")
    }

}

在我的演讲,我有:

import Foundation

class MainPresenter: MainPresenterProtocol {

    var screenViewController: MainViewProtocol?

    func presenterProtocolFuncTwo(numOne: Int, numTwo: Int, sucssesMessage: String, failMessage: String) -> String {
        print("function is called")
        return "presenter function is called sussfuly"
    }

}

和我的协议本身:

protocol MainViewProtocol {
    func showSmallHeadline(textToShow: String)
    func showHeadline(textToShow: String)
}

protocol MainPresenterProtocol {
    static func presenterProtocolFuncOne()
    func presenterProtocolFuncTwo(numOne: Int, numTwo: Int, sucssesMessage: String, failMessage: String) -> String
    func presenterProtocolFucThree () -> Bool
}

每当我打电话presenterProtocolFuncTwo,让我的默认值,并将它好好尝试去里面的功能在我的演讲

Answer 1:

你应该myPresenterscreenViewController性能与他们合作。

protocol MainViewProtocol: class { ... }

class FirstScreenViewController: UIViewController, MainViewProtocol {
    var myPresenter: MainPresenterProtocol?

    override func viewDidLoad() {
        super.viewDidLoad()
        myPresenter = MainPresenter(controller: self)
    }
}

class MainPresenter: MainPresenterProtocol {
    weak var screenViewController: MainViewProtocol?

    init(controller: MainViewProtocol) {
        screenViewController = controller
    }
}


Answer 2:

你需要一个对象分配给你的财产myPresenter地方在你的代码中viewDidLoad中,例如像

override func viewDidLoad() {
     super.viewDidLoad()
     myPresenter = MainPresenter()
}


文章来源: presenter function is never read on swift 3