Why is my List elements are deleted if I ON/OFF darkmode.

If I ON/OFF the darkmode with a button (by a bool value) on my view, my List elements are deleted. Not only do they disappear on the view, the list is actually becomes empty. The list declared in the AlarmModel, maybe thats the problem, I initialize an AlarmModel in the HomeView? How can I solve it?

struct ContentView: View {
    @State var isDarkMode = true
    var body: some View {
        NavigationView {
            HomeView(isDarkMode: self.$isDarkMode)
                .navigationBarTitle("")
                .navigationBarHidden(true)
        }.preferredColorScheme(self.isDarkMode ? .dark : .light)
    }
    
    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            ContentView()
        }
    }
    
    struct HomeView : View {
        @Binding var isDarkMode: Bool
        @State var isEditMode = false
        @ObservedObject var alarmModel : AlarmModel = AlarmModel()
        @ObservedObject var clockViewModel : ClockViewModel = ClockViewModel()
        
        var body: some View {
            GeometryReader { proxy in
                VStack(/*spacing: 15*/) {
                    TopBarView(alarmModel: self.alarmModel, clockViewModel: self.clockViewModel, isDarkMode: self.$isDarkMode, isEditMode: self.$isEditMode)
                    ClockView(clockViewModel: self.clockViewModel, isEditMode: self.$isEditMode)
                    AlarmListView(alarmModel: self.alarmModel).opacity(self.isEditMode ? 0 : 1)
                }
            }
        }
    }
}
  • The list declared in the AlarmModel, maybe thats the problem, I initialize an AlarmModel in the HomeView? How can I solve it? To solve it, please show more code. Where is the on/off button ? Could you also show the code of AlarmModel if it has changed from previous post.

  • The AlarmModel hasn't changed from the previous post. The button is in my TopBarView, and thats just toggle the isDarkMode bool. Button looks like: Button(action: {self.isDarkMode.toggle()}) { //there is just an image }. So I'm almost certain the problem is because I initialize an AlarmModel in the HomeView. I did this because the HomeView calls the other Views (and passes the initialized variables to the called Views), this is the only way I could connect the List (and other bool variables).

  • I'm sure there is a better solution for data binding, but this opportunity what I've found to share data (Swift UI v.1). Let me know if you need any more information about the code.

Add a Comment

Replies

@ObservedObject can only be used for objects initialized outside of the view hierarchy. Use @StateObject instead.

  • For an explanation see https://levelup.gitconnected.com/state-vs-stateobject-vs-observedobject-vs-environmentobject-in-swiftui-81e2913d63f9 and https://www.hackingwithswift.com/quick-start/swiftui/understanding-property-wrappers-in-swift-and-swiftui

  • Thanks for your answer, but I cant use @StateObject, because I use Swift UI v.1.

Add a Comment

Did you try using environmentObject for both alarmModel and clockViewModel ?