0

我已将我的 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

4

1 回答 1

2

EnvironmentObject需要传递给其他环境中的视图.environmentObject并声明为@EnvironmentObjectView (而不是 a @State


  1. 首先你需要更换:
@State var selectedTask = TaskElement(title: "", dateFrom: "", dateTo: "", text: "")

和:

@EnvironmentObject var selectedTask: TaskElement

(现在您TaskElement在所有视图中使用相同的实例)。

  1. 然后以与将 EnvironmentObject 传递给相同的方式ContentView
ContentView()
    .environmentObject(TaskElement(title: "", dateFrom: "", dateTo: "", text: ""))

您需要将其传递给其他环境(工作表、fullScreenCover 等):

NewTaskView(isShown: $shown)
    .environmentObject(selectedTask)
于 2020-10-15T20:23:25.563 回答