I am new to VIPER and I have build the most simplest demo in that architecture, but I have trouble, as UILabel in View/View Controller is not updated. This is the relevant code: View:
override func viewDidLoad() {
super.viewDidLoad()
presenter?.updateView()
}
Presenter:
func updateView() {
interactor?.getText()
}
Interactor:
func getText() {
presenter?.gotText(text: "Hello Viper")
}
Presenter:
func gotText(text: String) {
view?.updateLabel(text: text)
}
View:
func updateLabel (text: String) {
print(text)
helloLabel.text = text
}
print(text) returns the value, but the view itself is not updated.
I tried with updating on the main thread, but with no luck. I have seen some project s with the similar pattern (Updating a view in ViewController /label/backgroundColor etc), but I can't figure out what is wrong with my code.
The sample is available at GitHub https://github.com/veliborsantic/VIPER-simple-example The similar project is available at https://github.com/smalam119/live-news-viper, which runs as expected.
EDIT 1: I realized that viewDidLoad func is called twice. I set print messages in both viewDidLoad() and updateLabel (text: String) as follows:
override func viewDidLoad() {
super.viewDidLoad()
print("viewDidLoad: ", helloLabel.text!)
presenter?.updateView()
}
func updateLabel (text: String) {
helloLabel.text = text
print("updateLabel: ", helloLabel.text!)
}
Outputs:
viewDidLoad:
updateLabel: Hello Viper
viewDidLoad:
If I remove configuration for initialViewController in AppDelegate, viewDidLoad is called once, but the module is not loaded.
EDIT 2 / SOLVED After researching, I had to make 3 things: Tell that I do not want initializing controller via storyboard, so: 1. Turn off "is Initial View Controller" 2. Deattach Main in Deployment Info and 3. All code from AppDelegate move to Scene Delegate in func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)