我将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
答案 0 :(得分:2)
EnvironmentObject
需要作为.environmentObject
传递给其他环境中的视图,并在视图中声明为@EnvironmentObject
(而不是@State
)
@State var selectedTask = TaskElement(title: "", dateFrom: "", dateTo: "", text: "")
具有:
@EnvironmentObject var selectedTask: TaskElement
(现在您在所有视图中都使用相同的TaskElement
实例)。
ContentView
相同的方式:ContentView()
.environmentObject(TaskElement(title: "", dateFrom: "", dateTo: "", text: ""))
您需要将其传递到其他环境(工作表,fullScreenCover等):
NewTaskView(isShown: $shown)
.environmentObject(selectedTask)