I have looked at a few similar issues, but none were close enough to my state to work. I have 3 files:
- ContentView.swift
- VariableSetupModel.swift
- ContentNavView.swift
Previously the code in all 3 were in a single file (ContentView). I am now working on splitting it up to make the code cleaner. I am a fairly novice iOS developer. I moved all of my variables to the VariableSetupModel and got that working fine. I reference values in that file, they update, and return to the ContentView to update the views.
Now I am working on splitting my views on the same page into multiple files, starting with the nav. The view shows up, reading values from the VariableSetupModel correctly when the page first opens. But when the value changes from a button in ContentView, it does not update the title on the view in ContentNavView. Below is a simplified version of the code I have.
ContentView.swift
struct ContentView: View {
//Pull in variables from "VariableSetupModel"
@ObservedObject var viewModel = VariableSetupModel()
@State var counter:Int = 0
var body: some View {
//Main Views
VStack (alignment: .leading) {
// MARK: Climbs-Title
ContentNavView()
}
}
Button(action: {
if viewModel.toggleListGraphs == "graph" {
viewModel.toggleListGraphs = "list"
}else{
viewModel.toggleListGraphs = "graph"
}
}) {
Image(systemName: "list.bullet.below.rectangle")
.font(.title)
}
}
VariableSetupModel.swift
final class VariableSetupModel: ObservableObject {
static let shared = VariableSetupModel()
//Visuals
@Published var toggleListGraphs = "graph"
}
ContentNavView.swift
struct ContentNavView: View {
//Pull in variables from "VariableSetupModel"
@ObservedObject var viewModel = VariableSetupModel()
//var viewModel = VariableSetupModel.shared
var body: some View {
HStack{
if viewModel.toggleListGraphs == "graph"{
Text("All Sends")
.font(.title)
.padding(.horizontal)
}else{
Text("All Climbs")
.font(.title)
.padding(.horizontal)
}
Spacer()
Button(action: {
viewModel.showSettingsModal = true
}) {
Image("logoIcon")
.renderingMode(.original)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 35, height: 35)
}.padding().foregroundColor(Color("whiteblack"))
}.sheet(isPresented: $viewModel.showSettingsModal, onDismiss: {
//print(viewModel.showSettingsModal)
}) {
SettingsView()
}
}
}