Home > Back-end >  SwiftUI view not changing on state change
SwiftUI view not changing on state change

Time:11-27

im working on an app that I wish would have more then one view. I created a class as a ObservableObject to be able to access the same variables from multiple views files. I want to change one bool on a text click and because of that bool change i want the view to change. Here are my files:

MoodyApp.swift

import SwiftUI

@main
struct MoodyApp: App {
    @State var globalVars = vars
    var body: some Scene {
        WindowGroup {
            if(vars.isSettings){
                SettingsView()
            }
            else{
                MainView()
            }
        }
    }
}

The main view


import SwiftUI

struct MainView: View {

    //colors
    let darkGrey : Color = Color(red: 62/255, green: 62/255, blue: 62/255);
    let grey : Color = Color(red: 86/255, green: 86/255, blue: 86/255);
    let lightGrey : Color = Color(red: 117/255, green: 117/255, blue: 117/255);
    let dayGrey : Color = Color(red: 206/255, green: 206/255, blue: 206/255);
    let white : Color = Color(red: 242/255, green: 242/255, blue: 242/255);
    
    let years = Callendar();
    
    var body: some View {
        VStack(){
            VStack{
                HStack{
                    Text("menu")
                        .foregroundColor(white)
                        .padding()
                    Spacer()
                    Text("settings")
                        .foregroundColor(white)
                        .padding()
                        .onTapGesture(){
                            //HERE I WANT A BOOL TO CHANGE AND THE APP TO UPDATE THE VIEW
                        }
                }
                .ignoresSafeArea()
                .frame(height: 0.0)
                HStack{
                    Text("2023")
                        .font(.system(size:36))
                        .foregroundColor(white)
                }
            }.background(darkGrey)
            ScrollView(showsIndicators: false){
                ForEach(0..<12) { index in
                    Text(years.month[index])
                        .font(.system(size:36))
                        .foregroundColor(white)
                        .padding(.bottom, -8)
                        ZStack{
                            RoundedRectangle(cornerRadius: 45, style: .continuous)
                                .fill(lightGrey)
                                .frame(width: 320, height: 320)
                            ForEach(0..<years.quantity[index], id: \.self){ value in
                                let x = CGFloat(years.positionX[index][value])
                                let y = CGFloat(years.positionY[index][value])
                                RoundedRectangle(cornerRadius: 7, style: .continuous)
                                    .fill(dayGrey)
                                    .frame(width: 30, height: 30)
                                    .position(x: x, y: y);
                                Text(String(value 1))
                                    .position(x: x, y: y)
                                    .foregroundColor(.black);
                            }
                        }
                    
                    }
                }
        }.background(grey)
            .statusBar(hidden: true)
    }

}

struct MainView_Previews: PreviewProvider {
static var previews: some View {
MainView()
}
}

And here is the globalVars


import Foundation
class GlobalVars: ObservableObject {
@Published var isSettings = false
}

var vars = GlobalVars()

The variable is changing correctly, i tried to change it after click and printing it in console. it seems like the main App file is not recognizing the change?? i dont really know how to work this problem around, i've started working in swift recently. Any help would be great.

CodePudding user response:

The issue is here:

@State var globalVars = vars

is wrong. It should be a @StateObject:

@StateObject var globalVars = vars

and triggering it would be like:

vars.isSettings = true

Remarks:

While this approach with the global var may work, it is not a very swifty way to solve this. You should initialize a GlobalVars instance in your App struct and then pass it on to your Views environment. Then pull it from the environment and manipulate it.

e.g.:

struct MoodyApp: App {

    @StateObject var globalVars = GlobalVars()

    var body: some Scene {
        WindowGroup {
        if(globalVars.isSettings){
            SettingsView()
                .environmentObject(globalVars)
        }
        else{
            MainView()
                .environmentObject(globalVars)
        }
    }
}

and then in the View:

@EnvironmentObject private var globalVars: GlobalVars

and in the onTapGesture:

globalVars.isSettings = true
  • Related