英文:
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论