0

in my current project I work with an EnvironmentObject but I have a very strange bug, maybe someone can help.

First the code:

Model:

class Daten: ObservableObject{

@Published var intervallInsgesamt: Double = UserDefaults.standard.double(forKey: Keys.intervallInsgesamt){
    didSet{
        UserDefaults.standard.set(self.intervallInsgesamt, forKey: Keys.intervallInsgesamt)
    }
}

@Published var pauseInsgesamt: Double = UserDefaults.standard.double(forKey: Keys.pauseInsgesamt ){
    didSet{
        UserDefaults.standard.set(self.pauseInsgesamt, forKey: Keys.pauseInsgesamt)
    }
}
... 
}

First View:

@EnvironmentObject var daten: Daten

var body: some View {
    NavigationView{
        GeometryReader { geometry in
            VStack{
                ScrollView(showsIndicators: false){
                    ...
                }

               //IMPORTANT START
                NavigationLink(destination: TrainingView().environmentObject(daten)){ //!!!
                    Text("Start")
                        .styleButton()
                 }
               //IMPORTANT END
            }
        }
    }

}

Second View (TraingsView):

struct TrainingView: View {

@EnvironmentObject var daten: Daten

@State private var isRunning = false
@State private var to: CGFloat = 0

@State private var insgesamt = 0.0
@State private var minuten = 0
@State private var sekunden = 0
@State private var durchgang = 1

@State private var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()

@State private var zähl = 0

@State private var color = Color.red

@State private var übung = 0

@State private var momentaneÜbung = ""
@State private var nächsteÜbung = ""

@State private var teilerTo = 0

var body: some View {
    ZStack{
        VStack{
            ...
            }
            HStack(spacing: 50){
                
                Button(action:{

                    isRunning = true
                    Sounds.playSounds(soundfile: "sound.wav")
                    
                }) {
                    Text("Start")
                }
                
                Button(action:{
                    isRunning = false
                    Sounds.playSounds(soundfile: "sound.wav")
                }) {
                    Text("Stop")
                }
            }
        }
    }
    .onReceive(timer) { _ in
        if isRunning{
            if insgesamt > 0{
                print("1. Durchgang: \(durchgang), Übung: \(übung), momenaten: \(momentaneÜbung), nächste: \(nächsteÜbung)")
                insgesamt -= 1.0
                minuten = Int(insgesamt/60)
                sekunden = Int(insgesamt.truncatingRemainder(dividingBy: 60))
                zähl += 1
                
                withAnimation(.default) {
                    to = CGFloat(Double(zähl) / Double(teilerTo))
                    
                }
            }else{
                ... stuff that should run when insgesamt <= 0 but is not important for my problem 
            }
        }
    }
    .onAppear {
        teilerTo = Int(daten.intervallInsgesamt)
        print("Teiler: \(teilerTo)")
        insgesamt = daten.intervallInsgesamt
        
        minuten = Int(insgesamt/60)
        sekunden = Int(insgesamt.truncatingRemainder(dividingBy: 60))
        
        if !daten.übungen.isEmpty{
            momentaneÜbung = daten.übungen[übung].name
            if übung+1 < daten.übungen.count{
                nächsteÜbung = "Pause"
            }else{
                nächsteÜbung = "Nächster Durchgang"
            }
        }
    }
}
}

NOW THE PROBLEM: When I start the app, set the different times, go to the TrainingsView, everything works fine. Then I go back to the first View(change the times or not, it doesn't matter) and go back to the TraingsView. Now nothing works! It doesn't find the EnvironmentObject anymore. I don't know why because the first time it did. Does somebody know why?

(An other problem I is, that print() doesn't work. I can't see the things that should print out. I don't know if that is important...)

Thanks in advance!

4

1 回答 1

0

在您的根视图中,您应该将您的对象声明为可观察对象:

@ObservableObject var daten = Daten()

那么你应该.environmentObject()在你的第一个视图之上使用一个修饰符:

// pass the daten object that was declared as observable object here
.environmentObject(daten)

从现在开始,在其他后代视图上,您​​可以通过将模型声明为环境对象来获取模型:

@EnvironmentObject var daten: Daten 
// gives you the same old daten model
于 2020-08-21T21:22:01.033 回答