So I'm currently getting to grips with swift and swiftui and have run into a problem with Observable objects. I want to be able to share the value of a variable over 2 different views so opted to use an Observable Object. The duration and interval variables will be set using a slider on the settings view, which will then be used for logic in my main view.
struct Settings: View {
@EnvironmentObject var appInfo: AppInformation
var body: some View {
NavigationView {
ZStack {
VStack (spacing: 35){
HStack {
Text("Duration")
.font(.system(size: 23, weight: .bold, design: .rounded))
.padding(.leading, 25)
Spacer()
Slider(value: appInfo.duration, in: 1...60, step: 1)
.padding(.trailing, 20)
.padding(.leading, 20)
Text("\(Int(appInfo.duration))")
.padding(.trailing, 30)
.font(.system(size: 23, weight: .medium, design: .rounded))
}
.padding(.top, 100)
HStack {
Text("Interval ")
.font(.system(size: 23, weight: .bold, design: .rounded))
.padding(.leading, 25)
Spacer()
Slider(value: appInfo.interval, in: 1...60, step: 1)
.padding(.trailing, 20)
.padding(.leading, 20)
Text("\(Int(appInfo.interval))")
.padding(.trailing, 30)
.font(.system(size: 23, weight: .medium, design: .rounded)).navigationTitle("Settings")
}
Spacer()
}
}
}
}
struct Settings_Previews: PreviewProvider {
static var previews: some View {
Settings()
.environmentObject(AppInformation())
}
}
}
class AppInformation: ObservableObject {
var duration = 0.0
var interval = 0.0
}
But when attempting to either preview or sun my app in the simulator, it throws an error 'A View.environmentObject(_:) for AppInformation may be missing as an ancestor of this view.'
It throws the error on these lines where I'm referencing the variable as a slider value, even though I have referenced the variable in a text view further down which didn't seem to have any problems.
.padding(.trailing, 20)
.padding(.leading, 20)
Text("\(Int(appInfo.duration))")
.padding(.trailing, 30)
.font(.system(size: 23, weight: .medium, design: .rounded))
Slider(value: appInfo.interval, in: 1...60, step: 1)
.padding(.trailing, 20)
.padding(.leading, 20)