如何将数据从一个模型传递到另一个模型?

How to pass data from one model to another model?

我有两个不同的视图,每个视图都有一个模型来保存它们的数据。我正在尝试传递模型中一个变量的值以用于另一个模型,但未传递第一个模型的值。

第一个文件

struct Number: View {

@StateObject var model = NumberModel()

var body: some View {
    NavigationView {
        Form {
            Section {
                TextField("Enter your first number", text: $model.firstNum)
                    .keyboardType(.decimalPad)
            }
            Section {
                Text("\(model.firstNum)")
            }
        }
    }
}
}

第一个文件的模型

class NumberModel: ObservableObject {
@Published var firstNum: String

init() {
    self.firstNum = ""
}
}

第二个文件

struct SecondNumber: View {

@StateObject var model = SecondNumberModel()

var body: some View {
    NavigationView {
        Form {
            Section {
                TextField("Enter your second number", text: $model.secondNum)
                    .keyboardType(.decimalPad)
            }
            Section {
                Button {
                    model.add()
                } label: {
                    Text("Press Me!!!")
                }

            }
            Section {
                Text("\(model.total)")
            }
        }
    }
}
}

第二个文件的模型

class SecondNumberModel: ObservableObject {
@ObservedObject var model = NumberModel()

@Published var secondNum: String

@Published var total: Int

init() {
    self.secondNum = ""
    self.total = 0
}

func add() {
    self.total = Int(self.secondNum + self.model.firstNum) ?? 0
}
}

这是内容视图

struct ContentView: View {
var body: some View {
    TabView {
        Number()
            .tabItem {
                Image(systemName: "circle.fill")
                Text("First")
            }
        SecondNumber()
            .tabItem {
                Image(systemName: "circle.fill")
                Text("Second")
            }
    }
}
}

我正在尝试从第一个文件中获取用户输入,然后将该数字发送到第二个文件以与收集到的第二个数字一起添加。但是第一个数字的值不会传递到第二个文件的模型中。感谢任何帮助。谢谢。

如果您有需要共享状态的兄弟视图,则该状态应由父视图控制。例如,这适用于您的情况:

class NumberModel: ObservableObject {
    @Published var firstNum: String = ""
    @Published var secondNum: String = ""
    @Published var total: Int = 0
    
    func add() {
        self.total = Int(self.secondNum + self.firstNum) ?? 0
    }
}

struct Number: View {
    @ObservedObject var model : NumberModel
    
    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Enter your first number", text: $model.firstNum)
                        .keyboardType(.decimalPad)
                }
                Section {
                    Text("\(model.firstNum)")
                }
            }
        }
    }
}

struct SecondNumber: View {
    
    @ObservedObject var model : NumberModel
    
    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Enter your second number", text: $model.secondNum)
                        .keyboardType(.decimalPad)
                }
                Section {
                    Button {
                        model.add()
                    } label: {
                        Text("Press Me!!!")
                    }
                    
                }
                Section {
                    Text("\(model.total)")
                }
            }
        }
    }
}

struct ContentView: View {
    @StateObject private var appState = NumberModel()
    
    var body: some View {
        TabView {
            Number(model: appState)
                .tabItem {
                    Image(systemName: "circle.fill")
                    Text("First")
                }
            SecondNumber(model: appState)
                .tabItem {
                    Image(systemName: "circle.fill")
                    Text("Second")
                }
        }
    }
}