gpt4 book ai didi

swift - 使用 VIPER 架构更新 View 中的标签

转载 作者:行者123 更新时间:2023-11-30 10:31:26 25 4
gpt4 key购买 nike

我是 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) 返回值,但 View 本身并未更新。

我尝试在主线程上进行更新,但没有成功。我见过一些具有类似模式的项目(更新 ViewController/label/backgroundColor 等中的 View ),但我无法弄清楚我的代码出了什么问题。

该示例可从 GitHub https://github.com/veliborsantic/VIPER-simple-example 获取。类似的项目可以在 https://github.com/smalam119/live-news-viper 找到。 ,它按预期运行。

编辑1:我意识到 viewDidLoad 函数被调用了两次。我在 viewDidLoad() 和 updateLabel (text: 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/已解决经过研究,我必须做三件事:告诉我不想通过 Storyboard初始化 Controller ,所以:1.关闭“是初始 View Controller ”2. 部署信息中Deattach Main和3. AppDelegate 中的所有代码都移动到 func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)

中的 Scene Delegate

最佳答案

设置文本后尝试 view.layoutIfNeeded()

关于swift - 使用 VIPER 架构更新 View 中的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59128914/

25 4 0