SwiftUI视图在状态更改时不更改

ljsrvy3e  于 2022-11-28  发布在  Swift
关注(0)|答案(1)|浏览(198)

我正在开发一个应用程序,我希望它有多个视图。我创建了一个ObservableObject类,以便能够从多个视图文件中访问相同的变量。我希望在文本点击时更改一个bool,因为这个bool的更改,我希望视图也随之更改。以下是我的文件:
MoodyApp.swift

import SwiftUI

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

主视图

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

这是全局变量

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

var vars = GlobalVars()

变量更改正确,我尝试更改后,点击并在控制台中打印它。它似乎是主要的应用程序文件不承认的变化??我真的不知道如何解决这个问题,我最近开始在swift工作。任何帮助将是伟大的。

qnzebej0

qnzebej01#

问题就在这里:

@State var globalVars = vars

错误。它应该是@StateObject

@StateObject var globalVars = vars

和触发它就像:

vars.isSettings = true

备注:

虽然这种使用全局var的方法可能有效,但它不是一种非常快速的解决方法。您应该在App结构体中初始化一个GlobalVars示例,然后将其传递到您的视图环境中。然后将其从环境中拉出来并对其进行操作。
例如:

struct MoodyApp: App {

    @StateObject var globalVars = GlobalVars()

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

然后在“视图:

@EnvironmentObject private var globalVars: GlobalVars

和在onTapGesture

globalVars.isSettings = true

相关问题