我是VIPER的新手,我已经建立了该体系结构中最简单的演示,但是我遇到了麻烦,因为未更新View / View Controller中的UILabel。这是相关代码:查看:
override func viewDidLoad() {
super.viewDidLoad()
presenter?.updateView()
}
主持人:
func updateView() {
interactor?.getText()
}
交互者:
func getText() {
presenter?.gotText(text: "Hello Viper")
}
演示者:
func gotText(text: String) {
view?.updateLabel(text: text)
}
查看:
func updateLabel (text: String) {
print(text)
helloLabel.text = text
}
print(text)返回值,但视图本身未更新。
我尝试对主线程进行更新,但是没有运气。我已经看到了一些具有类似模式的项目(在ViewController / label / backgroundColor等中更新视图),但是我无法弄清楚我的代码出了什么问题。
该示例可从GitHub https://github.com/veliborsantic/VIPER-simple-example获得。可以在https://github.com/smalam119/live-news-viper获得类似的项目,该项目将按预期运行。
编辑1:我意识到viewDidLoad函数被调用了两次。我在viewDidLoad()和updateLabel(文本:String)中都设置了打印消息,如下所示:
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:
如果我在AppDelegate中删除initialViewController的配置,则会调用一次viewDidLoad,但不会加载该模块。
编辑2 /已解决经过研究,我必须做三件事:告诉我我不想通过情节提要初始化控制器,因此:1.关闭“是初始视图控制器”2.取消主要部署信息和3.将来自AppDelegate的所有代码移至func场景中的Scene Delegate(_场景:UIScene,willConnectTo会话:UISceneSession,选项connectionOptions:UIScene.ConnectionOptions)
仅在设置文本后尝试view.layoutIfNeeded()