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

时间:2020-10-20 07:12:47

标签: uiscrollview swiftui environmentobject

那是我的项目链接https://github.com/m3rtkoksal/TaskManager

这是我的TaskListView

enter image description here

这是我的NewTaskView

enter image description here

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

那是我的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)
        })
    }
}

当在Scrollview上选择一个项目时,如何修改NewTaskView以使其能够查看selectedTask数据?

我尝试过如下操作,但是尝试添加新项目时出现索引超出范围错误。另外,必须有比使用其他方法更好的方法

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

1 个答案:

答案 0 :(得分:1)

您必须在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()
        }
    }