Swift 使用VIPER体系结构更新视图中的标签
我是VIPER的新手,我已经构建了该体系结构中最简单的演示,但我遇到了问题,因为视图/视图控制器中的UILabel没有更新。以下是相关代码: 视图: 演示者:Swift 使用VIPER体系结构更新视图中的标签,swift,label,viper,Swift,Label,Viper,我是VIPER的新手,我已经构建了该体系结构中最简单的演示,但我遇到了问题,因为视图/视图控制器中的UILabel没有更新。以下是相关代码: 视图: 演示者: func updateView() { interactor?.getText() } 交互者: func getText() { presenter?.gotText(text: "Hello Viper") } 演示者: func gotText(text: String) { view?.
func updateView() {
interactor?.getText()
}
交互者:
func getText() {
presenter?.gotText(text: "Hello Viper")
}
演示者:
func gotText(text: String) {
view?.updateLabel(text: text)
}
视图:
打印(文本)返回值,但视图本身不更新
我尝试更新主线程,但没有成功。我见过一些具有类似模式的项目(在ViewController/label/backgroundColor等中更新视图),但我不知道我的代码出了什么问题
该示例可从GitHub获得
类似的项目在上提供,按预期运行
编辑1:
我意识到viewDidLoad func被调用了两次。我将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场景中的场景代理(“场景:UIScene,willConnectTo会话:UISceneSession,options connectionOptions:UIScene.connectionOptions”)只需尝试查看。布局需要()设置文本后,我刚刚尝试过,没有任何进展。这对我来说真的很奇怪。只需mb this:view.setNeedsLayout()或view.label.layoutifneed()或bad way view.layoutSubviews()您可以展示您的线框或组装Viper模块所有层的方法吗?这就是原因
func updateLabel (text: String) {
print(text)
helloLabel.text = text
}
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: