我已将我的 TaskElement 模型设置为能够编辑任务的 environmentObject。我确实喜欢这样做,以便能够在同一页面上编辑任务以创建新任务。这称为NewTaskView。顺便说一句,我在这个 NewTaskView_Previews 中也遇到了一个错误,但我认为一旦我能理解以下两个错误原因,我就可以自己解决它。
let contentView = ContentView().environment(\.managedObjectContext,context)
.environmentObject(observer())
.environmentObject(TaskElement(title: "", dateFrom: "", dateTo: "", text: ""))
但是进行此更改时会出现以下错误。
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>'
})
}
}
和
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)
}
}
我在这里先向您的帮助表示感谢。如果您想查看整个项目,可以访问https://github.com/m3rtkoksal/TaskManager