如何在SwiftUI中实现MVVM模式?视图不会重新渲染。

huangapple go评论80阅读模式
英文:

How to implement the MVVM-Pattern in SwiftUI? The View is not re-rendered

问题

我正在学习SwiftUI,并尝试使用MVVM模式实现一个简单的计时器。但计时器视图没有重新渲染。问题在哪里?

// 计时器模型

import Foundation

class TimerModel {
    let label: String = "计数器"
    var count: Int = 0
}
// 计时器视图模型

import Foundation
import SwiftUI
import Combine

class TimerViewModel: ObservableObject {
    
    @Published var timerModel: TimerModel = TimerModel()
    
    var label: String {
        return self.timerModel.label
    }
    
    var count: Int {
        return self.timerModel.count
    }
    
    func startTimer() {
        Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
            
            DispatchQueue.main.async {
                self.timerModel.count += 1
            }
            
        }
    }
}
// 计时器视图

import SwiftUI
import Combine

struct TimerView: View {
    
    @ObservedObject var timerViewModel: TimerViewModel
    
    init() {
        self.timerViewModel = TimerViewModel()
    }
    
    var body: some View {
        VStack {
            Text("\(self.timerViewModel.label): \(self.timerViewModel.count)")
            Button(action: {
                self.timerViewModel.startTimer()
            }, label: {
                Text("开始")
            })
        }
    }
}

struct TimerView_Previews: PreviewProvider {
    static var previews: some View {
        TimerView()
    }
}
// 内容视图

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        TimerView()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

在计时器视图模型中,计时器模型的count属性每秒都在更改,但计时器视图没有重新渲染。感谢任何有关实现MVVM模式的想法。

英文:

i am learning SwiftUI and trying to implement a simple Timer with the MVVM-pattern. But the view of the Timer is not re-rendered. What is the problem?

// Timer Model

import Foundation

class TimerModel {
    let label: String = "Counter"
    var count: Int = 0
}
// Timer View Model

import Foundation
import SwiftUI
import Combine

class TimerViewModel: ObservableObject {
    
    @Published var timerModel: TimerModel = TimerModel()
    
    var label: String {
        return self.timerModel.label
    }
    
    var count: Int {
        return self.timerModel.count
    }
    
    func startTimer() {
        Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { (timer) in
            
            DispatchQueue.main.async {
                self.timerModel.count += 1
            }
            
        }
    }
}
// Timer View 

import SwiftUI
import Combine

struct TimerView: View {
    
    @ObservedObject var timerViewModel: TimerViewModel
    
    init() {
        self.timerViewModel = TimerViewModel()
    }
    
    var body: some View {
        VStack {
            Text("\(self.timerViewModel.label): \(self.timerViewModel.count)")
            Button(action: {
                self.timerViewModel.startTimer()
            }, label: {
                Text("Start")
            })
        }
    }
}

struct TimerView_Previews: PreviewProvider {
    static var previews: some View {
        TimerView()
    }
}
// Content View 

import SwiftUI

struct ContentView: View {
    
    var body: some View {
        TimerView()
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

The count property of the Timer Model in the Timer View Model is changed every second, but the Timer View is not re-rendered. Thanks for any idea to implement the MVVM-pattern.

答案1

得分: 1

最简单的方法是将模型更改为值类型

struct TimerModel {
    let label: String = "Counter"
    var count: Int = 0
}
英文:

The simplest is to change model to value type

struct TimerModel {
    let label: String = "Counter"
    var count: Int = 0
}

huangapple
  • 本文由 发表于 2020年1月6日 20:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612115.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定