I have set my TaskElement model to be an environmentObject to be able to edit tasks. I did like this to be able to edit tasks on the same page to create a new task. Which is called NewTaskView. Btw, I also get an error in this NewTaskView_Previews but I think I can solve it myself once I can understand below two errors reason.
let contentView = ContentView().environment(\.managedObjectContext,context)
.environmentObject(observer())
.environmentObject(TaskElement(title: "", dateFrom: "", dateTo: "", text: ""))
But making this change turned back at me with the following errors.
struct ScrollViewTask: View {
@ObservedObject private var obser = observer()
@State var selectedTask = TaskElement(title: "", dateFrom: "", dateTo: "", text: "")
@State var shown: Bool = false
var body: some View {
ScrollView(.vertical) {
VStack {
ForEach(self.obser.tasks) { task in
TaskElementView(task:task)
.onTapGesture {
self.selectedTask = task
print(task)
self.shown.toggle()
}
}
}
}
.onAppear {
self.obser.fetchData()
}
.fullScreenCover(isPresented: $shown, content: {
NewTaskView(isShown: $shown, task: selectedTask) //Cannot convert value of type 'TaskElement' to expected argument type 'EnvironmentObject<TaskElement>'
})
}
}
and
struct TaskListView: View {
@State private(set) var data = ""
@State var isSettings: Bool = false
@State var isSaved: Bool = false
@State var shown: Bool = false
@State var selectedTask = TaskElement(title: "", dateFrom: "", dateTo: "", text: "")
var body: some View {
NavigationView {
ZStack {
Color(#colorLiteral(red: 0.9333333333, green: 0.9450980392, blue: 0.9882352941, alpha: 1)).edgesIgnoringSafeArea(.all)
VStack {
TopBar()
HStack {
CustomTextField(data: $data, tFtext: "Find task", tFImage: "magnifyingglass")
Button(action: {
self.isSettings.toggle()
}, label: {
ZStack {
RoundedRectangle(cornerRadius: 15)
.frame(width: 50, height: 50, alignment: .center)
.foregroundColor(Color(#colorLiteral(red: 0.4274509804, green: 0.2196078431, blue: 1, alpha: 1)))
Image("buttonImage")
.resizable()
.frame(width: 30, height: 30, alignment: .center)
}
.padding(.horizontal, 15)
})
}
CustomSegmentedView()
ZStack {
TaskFrameView()
VStack {
Spacer()
HStack {
Spacer()
Button( action: {
self.isSaved.toggle()
}, label: {
ZStack {
RoundedRectangle(cornerRadius: 25)
.foregroundColor(Color(#colorLiteral(red: 1, green: 0.7137254902, blue: 0.2196078431, alpha: 1)))
Text("+")
.foregroundColor(.white)
.font(.title)
.fontWeight(.bold)
}
.frame(width: 50, height: 50)
})
}
}
NavigationLink(
destination: NewTaskView(isShown: $shown, task: selectedTask), // Cannot convert value of type 'TaskElement' to expected argument type 'EnvironmentObject<TaskElement>'
isActive: $shown,
label: {
Text("")
})
}
}
}
.navigationBarHidden(true)
Spacer()
}
.navigationBarHidden(true)
}
}
Thanks for the help in advance. If you want to see the whole project you can visit https://github.com/m3rtkoksal/TaskManager
The
EnvironmentObject
needs to be passed as.environmentObject
to views in other environments and declared as@EnvironmentObject
in a View (not as a@State
)with:
(Now you're using the same
TaskElement
instance in all views).ContentView
:you need to pass it to other environments (sheet, fullScreenCover etc.):