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

标签 swift label viper-architecture

我是 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/

相关文章:

ios - 通过在多个 Controller 之间准备 segue 来传递特定的键/值

c# - 为 Label 控件创建 KeyDown 事件处理程序

ios - 带自动换行的多行标签

ios - 毒蛇架构 : submodules in a module

viper-architecture - 如何使用 VIPER 架构设置异步图像加载?

ios - 我可以通过快速单击我的应用程序中的按钮将图片发布为 whatsapp 状态、信使或 instagram 故事吗

swift - 为什么不能在 map 语句中使用键路径语法?

objective-c - 如何识别/标记 OS X 应用程序实例

highcharts - 在 Highcharts 中仅显示第一个和最后一个 xAxis 标签

ios - 如何让 Xcode 识别自定义模板