我有一个简单的用例,有一个动态数量的文本的 VStack,其中切换按钮来自一个数组。
import SwiftUI
public struct Test: View {
@ObservedObject public var viewModel = TestViewModel()
public init() {
}
public var body: some View {
VStack {
ForEach(viewModel.models) { model in
ToggleView(title: <#T##Binding<String>#>, switchState: <#T##Binding<Bool>#>)
//how to add the above
}
}.padding(50)
}
}
struct ToggleView: View {
@Binding var title: String
@Binding var switchState: Bool
var body: some View {
VStack {
Toggle(isOn: $switchState) {
Text(title)
}
}
}
}
public class TestModel: Identifiable {
@Published var state: Bool {
didSet {
//do something
//publish to the view that the state has changed
}
}
@Published var title: String
init(state: Bool, title: String) {
self.state = state
self.title = title
}
}
public class TestViewModel: ObservableObject {
@Published var models: [TestModel] = [TestModel(state: false, title: "Title 1"), TestModel(state: true, title: "Title 2")]
}
出现以下问题:
- 在 MVVM 模式中,绑定变量可以放在模型类中还是应该放在视图模型中?
- 当切换状态发生变化时,如何将状态变化的消息从模型类发送到视图/场景?
- 如果在视图模型中为每个切换状态使用绑定变量数组,如何知道数组的哪个特定元素已更改?(见下面的代码片段)
class ViewModel {
@Published var dataModel: [TestModel]
@Published var toggleStates = [Bool]() {
didSet {
//do something based on which element of the toggle states array has changed
}
}
}
请帮忙解答以上问题。