I have a Companion WatchOS App with a WatchViewModel, that currently holds all my variables that get send from the App. I have to views, each should display another variable. In one view (ActivityDistributionView) everything works perfectly fine, but in the other one (LeaderboardView) the value is the init value and is not getting changed. Actually no value gets updated in the LeaderboardView, but in the ActivityDistributionView, all values work fine.
ContentView body:
var body: some View {
TabView{
ScoreTabView(dailyScore: viewModel.scores["score"] ?? 0, postureScore: viewModel.scores["posture"] ?? 0, movementScore: viewModel.scores["movement"] ?? 0)
NavigationStack{
List{
Section("Last message received:"){
Text("(formatDate(date: viewModel.lastMessageTime))").font(.body)
//Text("(viewModel.lastMessage.keys.sorted().formatted())").padding()
}
NavigationLink("Leaderboard"){
LeaderboardViews()
}
NavigationLink("Activity Distribution"){
ActivityDistributionView()
}
}
}
}
}
struct LeaderboardViews: View {
@State private var viewModel: WatchViewModel = WatchViewModel()
var body: some View {
Text("(viewModel.leaderboardMessage)")
/*ForEach(leaderboardData.payload.keys.sorted(), id: .self) { date in
Section(header: Text(date)) {
ForEach(leaderboardData.payload[date]!, id: .self) { exerciseDataWrapper in
VStack(alignment: .leading) {
Text("Name: (exerciseDataWrapper.name)")
}
.padding(.vertical)
}
}
}*/
}
}
struct ActivityDistributionView: View {
@State private var viewModel: WatchViewModel = WatchViewModel()
@State private var vibrates = false
let activities = ["sitting", "walking", "standing", "onDesk", "driving", "lying"]
var body: some View {
NavigationStack{
ScrollView{
Text("(viewModel.leaderboardMessage)")
}
.navigationTitle("Acitivity Distribution")
.navigationBarTitleDisplayMode(.inline)
}
}
}
I tried changing the name and the location of the NavLink or trying to pass the viewModel value from my ContentView through to my LeaderboardView but it didn’t work.