在添加新项目视图 SwiftUI 中显示选定的 ScrollView 项目

Show Selected ScrollView Item in Add New Item View SwiftUI

那是我的项目linkhttps://github.com/m3rtkoksal/TaskManager

这是我的 TaskListView

这是我的 NewTaskView

不幸的是,当我再次点击滚动视图中的一项时,我看到了一个空的 NewTaskView。我应该看到带有 selectedTask 数据的 NewTaskView。

那是我的 NewTaskView

struct NewTaskView: View {
@Environment(\.presentationMode) private var presentationMode
@StateObject private var obser = observer()
@State var taskTitle = ""
@State var taskFrom = ""
@State var taskFromDate = Date()
@State var taskToDate = Date()
@State var taskTo = ""
@State var taskNote = ""
@EnvironmentObject var task: SelectedTask
var body: some View {
    NavigationView {
    VStack(alignment: .leading) {
        Group {
            Text("Task Title")
            TextField("Title", text:$taskTitle)
            Divider()
            Text("From")
            DatePicker("", selection: $taskFromDate, in: Date()..., displayedComponents: [.date, .hourAndMinute])
                .labelsHidden()
            .onReceive(Just(taskFromDate)) { data in
                    taskFrom = getDate(date: taskFromDate)
            }
            Divider()
        } .padding(.horizontal, 10)
        .font(Font.custom("SFCompactDisplay-Bold", size: 25))
        .foregroundColor(.gray)
        Group {
            Text("To")
            DatePicker("", selection: $taskToDate, in: Date()..., displayedComponents: [.date, .hourAndMinute])
                .labelsHidden()
            .onReceive(Just(taskToDate)) { data in
                    taskTo = getDate(date: taskToDate)
            }
            Divider()
            Text("Note")
            TextField("Note", text:$taskNote)
        }.padding(.horizontal, 10)
        .font(Font.custom("SFCompactDisplay-Bold", size: 25))
        .foregroundColor(.gray)
        
        Button(action: {
            let taskDictionary = [
                "title" : self.taskTitle,
                "dateTo": self.taskTo,
                "dateFrom" : self.taskFrom,
                "text": self.taskNote
            ]
            let docRef = Firestore.firestore().document("tasks/\(UUID().uuidString)")
            docRef.setData(taskDictionary) { (error) in
                if let error = error {
                    print("error = \(error)")
                } else {
                    print("success")
                    self.taskTitle = ""
                    self.taskNote = ""
                }
            }
        }, label: {
            ZStack {
                RoundedRectangle(cornerRadius: 25)
                    .foregroundColor(Color(#colorLiteral(red: 0.4274509804, green: 0.2196078431, blue: 1, alpha: 1)))
                Text("Create task")
                    .foregroundColor(.white)
                    .font(.title)
                    .fontWeight(.bold)
            }
            .padding(.horizontal, 10)
            .frame(width: UIScreen.main.bounds.width - 20, height: 90)
        })
    }
}
    
    .navigationBarTitle("Create a task")
        .font(Font.custom("SFCompactDisplay-Bold", size: 30))
    }
  }

这就是我附加 selectedTask 并调用 NewTaskView()

的方式
import SwiftUI

struct TaskFrameView: View {
    @ObservedObject private var obser = observer()
    var body: some View {
        VStack(alignment: .leading){
            Text("Today task")
                .padding()
            ScrollViewTask()
        }
    }
}

struct ScrollViewTask: View {
    @EnvironmentObject var selectedTask : SelectedTask
    @State var shown: Bool = false
    @ObservedObject private var obser = observer()
    var body: some View {
        
        ScrollView(.vertical) {
            VStack {
                ForEach(self.obser.tasks) { task in
                    TaskElementView(task:task)
                        .onTapGesture {
                            self.shown.toggle()
                            self.selectedTask.appendNewTask(task: task)
                        }
                }
            }
        }
        .onAppear {
            self.obser.fetchData()
        }
        .fullScreenCover(isPresented: $shown, content: {
            NewTaskView()
                .environmentObject(selectedTask)
        })
    }
}

我应该如何修改 NewTaskView 才能在 Scrollview 上选择项目时看到 selectedTask 数据?

我已经尝试过如下所示,但是当我尝试添加新项目时出现索引超出范围错误。另外,肯定有比 if else

更好的方法
VStack(alignment: .leading) {
        Group {
            Text("Task Title")
            if task.item[0].title == "" {
            TextField("Title", text:$taskTitle)
            } else {
                TextField(task.item[0].title, text: $taskTitle)
            }

您必须在 SceneDelegate.swift

中添加这些代码
var selectedTask = SelectedTask()       

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        if let windowScene = scene as? UIWindowScene {
            let window = UIWindow(windowScene: windowScene)
            window.rootViewController = UIHostingController(rootView: ContentView()
                .environmentObject(selectedTask)
            )
            self.window = window
            window.makeKeyAndVisible()
        }
    }