I am writing a Swift program and am trying to get one of the previews to work but with no luck. The error message I get is: Program crashed due to missing environment of type NavigationViewModel. To resolve add .envionmentObject(NavigationViewModel(...)) to the appropriate preview. I have tried several things with no luck. If anyone could point out my error it would be appreciated. Below is my code. The preview section is at the bottom. Regards Chris
import SwiftUI
struct ListView: View {
@EnvironmentObject var navVM: NavigationViewModel
var body: some View {
Spacer().frame(height: 30)
VStack {
ForEach (navVM.options, id: \.self) { option in
switch option.id {
case 0:
HomeItem(imageName: option.imageName, title: option.title, id: option.id)
case 1, 4, 7:
MainItem(imageName: option.imageName, title: option.title, id: option.id)
case 2, 3, 5, 6, 8, 9:
SubItem(title: option.title, id: option.id)
default:
Text("Something went wrong")
} // end switch
} // end for each
} // end vstack
Spacer()
.frame(width: 180, alignment: .leading)
} // end var body
} // end list view
struct HomeItem: View {
let imageName : String
let title : String
let id: Int
// let currentViewId: Int
@EnvironmentObject var navVM: NavigationViewModel
var body: some View {
HStack{
Image(systemName: imageName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30, alignment: .leading)
.foregroundColor(navVM.highLightedImage == id ? Color.blue : Color.black)
.padding(.leading, 20 )
.padding(.top, 10)
Text(title)
.font(.system(size: 15))
.underline()
.frame(width: 125, alignment: .leading)
.foregroundColor(Color.blue)
.padding(.top, 15)
.onTapGesture {
navVM.updateCurrentViewId(value: id)
navVM.updateSelectedItem(value: 0)
navVM.updateHighLightedImage(value: 0)
}
} // end hstack
}
}
struct MainItem: View {
let imageName : String
let title : String
let id: Int
@EnvironmentObject var navVM: NavigationViewModel
var body: some View {
HStack{
Image(systemName: imageName)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 30, height: 30, alignment: .leading)
.foregroundColor(navVM.highLightedImage == id ? Color.blue : Color.black)
.padding(.leading, 20 )
.padding(.top, 10)
Text(title)
.font(.system(size: 15))
.frame(width: 125, alignment: .leading)
.padding(.top, 10)
} // end hstack
}
}
struct SubItem: View {
let title: String
// let currentViewId : Int
let id: Int
@EnvironmentObject var navVM: NavigationViewModel
var body: some View {
HStack{
Text(title)
.font(.system(size: 13))
.underline()
.frame(width: 35, alignment: .leading)
.foregroundColor(Color.blue)
.padding(.bottom, 2)
.padding(.leading, 15)
.onTapGesture {
navVM.updateCurrentViewId(value: id)
navVM.updateSelectedItem(value: id)
switch navVM.selecteditem {
case 2, 3:
navVM.updateHighLightedImage(value: 1)
case 5, 6:
navVM.updateHighLightedImage(value: 4)
case 8, 9:
navVM.updateHighLightedImage(value: 7)
default:
navVM.updateHighLightedImage(value: 0)
}
}
switch navVM.currentViewId {
case id:
Image(systemName: "checkmark").padding(.bottom, 5).foregroundColor(Color.blue).opacity(100)
default:
Image(systemName: "checkmark").padding(.bottom, 5).foregroundColor(Color.blue).opacity(0)
}
} // end hstack
}
}
struct ListView_Previews: PreviewProvider {
@EnvironmentObject var navVM: NavigationViewModel
static var previews: some View {
ListView()
}
// .environmentObject(navVM: NavigationViewModel())
}
CodePudding user response:
Add it to the ListView()
not the previews
variable
ListView().environmentObject(navVM: NavigationViewModel())