admin管理员组

文章数量:1357273

This is my simple View:

struct StartView: View {
    @State var viewModel = StartViewModel()
    var body: some View {
        Button(viewModel.settings.gameInProgress ? "1" : "2") {
            viewModel.startGame()
        }
    }
}

@Observable
class StartViewModel {
    var settings = Settings.shared
    
    func startGame() {
        settings.gameInProgress = true
    }
}

class Settings: ObservableObject {
    static var shared = Settings()
    @State var gameInProgress = false
    // here is a lot of code that manage above property and not only
}

When I press my "2" button, nothing changes, but it should be replaced to "1". Why doesn't it work?

PS gameInProgress must be kept in Settings because it will be accessed from many SwiftUI Views through its ViewModels.

本文标签: