Home > Back-end >  How to pass data from one model to another model?
How to pass data from one model to another model?

Time:10-28

I have two different views each with a model to hold their data. I'm trying to pass the value of one variable in the model to be used in the other model but the value from the first model isn't being passed.

First file

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)")
            }
        }
    }
}
}

Model for first file

class NumberModel: ObservableObject {
@Published var firstNum: String

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

Second file

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)")
            }
        }
    }
}
}

Model for second file

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
}
}

This is the content view

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

I'm trying to get user input from the first file and then send that number to the second file to be added with the second number gathered. But the value of the first number doesn't get passed into the second file's model. Appreciate any help. Thanks.

CodePudding user response:

If you have sibling views that need to share state, that state should be controlled by the parent view. For example, this would work in your case:

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")
                }
        }
    }
}
  • Related