presenter function is never read on swift 3

2019-08-17 14:53发布

I'm calling my presenter functon that is listed in a protocol that my presenter is implementing but whenever i call it it never goes inside, how do i initilize my presenter so i can call the interface funcion ?

here is my view controller:

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")
    }

}

in my presenter i have:

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"
    }

}

and my protocol itself:

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
}

whenever i call the presenterProtocolFuncTwo, i get my default value and it doens't go inside the function in my presenter

2条回答
The star\"
2楼-- · 2019-08-17 15:34

You should myPresenter and screenViewController properties to work with them.

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
    }
}
查看更多
放我归山
3楼-- · 2019-08-17 15:36

You need assign an object to your property myPresenter somewhere in your code, like in viewDidLoad for example

override func viewDidLoad() {
     super.viewDidLoad()
     myPresenter = MainPresenter()
}
查看更多
登录 后发表回答