简体   繁体   English

SwiftUi中视图之间的传递列表

[英]Passing List between Views in SwiftUi

I'm making a ToDo list app on my own to try to get familiar with iOS development and there's this one problem I'm having:我正在自己制作一个待办事项列表应用程序,以尝试熟悉 iOS 开发,我遇到了一个问题:

I have a separate View linked to enter in a new task with a TextField .我有一个单独的View链接以使用TextField输入新任务。 Here's the code for this file:这是此文件的代码:

import SwiftUI

struct AddTask: View {
     @State public var task : String = ""
     @State var isUrgent: Bool = false // this is irrelevant to this problem you can ignore it

    
    var body: some View {
        VStack {
            VStack(alignment: .leading) {
                Text("Add New Task")
                    .bold()
                    .font(/*@START_MENU_TOKEN@*/.title/*@END_MENU_TOKEN@*/)
                TextField("New Task...", text: $task)
                Toggle("Urgent", isOn: $isUrgent)
                    .padding(.vertical)
                Button("Add task", action: {"call some function here to get what is 
in the text field and pass back the taskList array in the Content View"})
               
                 
                
            }.padding()
            Spacer()
        }
    }
    
}


struct AddTask_Previews: PreviewProvider {
    static var previews: some View {
        AddTask()
    }
}

So what I need to take the task variable entered and insert it into the array to be displayed in the list in my main ContentView file.因此,我需要将输入的任务变量插入到数组中,以便在我的主ContentView文件的列表中显示。

Here's the ContentView file for reference:这是供参考的ContentView文件:

import SwiftUI

struct ContentView: View {
    @State var taskList = ["go to the bakery"]
    
    struct AddButton<Destination : View>: View {

        var destination:  Destination

        var body: some View {
            NavigationLink(destination: self.destination) { Image(systemName: "plus") }
        }
    }
    
    var body: some View {
        VStack {
            NavigationView {
                List {
                    ForEach(self.taskList, id: \.self) {
                    item in Text(item)
                    }
                }.navigationTitle("Tasks")
                .navigationBarItems(trailing: HStack { AddButton(destination: AddTask())})
            }
            
        }
        
}
struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
  }
}

I need a function to take the task entered in the TextField , and pass it back in the array in the ContentView to be displayed in the List for the user我需要一个 function 来获取在TextField中输入的任务,并将其传递回ContentView的数组中,以便在用户的List中显示

-thanks for the help -谢谢您的帮助

You can add a closure property in your AddTask as a callback when the user taps Add Task .当用户点击Add Task时,您可以在AddTask中添加一个闭包属性作为回调。 Just like this:像这样:

struct AddTask: View {
  var onAddTask: (String) -> Void // <-- HERE
  @State public var task: String = ""
  @State var isUrgent: Bool = false

  var body: some View {
    VStack {
      VStack(alignment: .leading) {
        Text("Add New Task")
          .bold()
          .font(.title)
        TextField("New Task...", text: $task)
        Toggle("Urgent", isOn: $isUrgent)
          .padding(.vertical)
        Button("Add task", action: {
          onAddTask(task)
        })
      }.padding()
      Spacer()
    }
  }

}

Then, in ContentView :然后,在ContentView

.navigationBarItems(
    trailing: HStack {
      AddButton(
        destination: AddTask { task in
            taskList.append(task)
        }
      )
  }
)

@jnpdx provides a good solution by passing Binding of taskList to AddTask . @jnpdx通过将 taskList 的Binding传递给AddTask提供了一个很好的解决方案。 But I think AddTask is used to add a new task so it should only focus on The New Task instead of full taskList.但我认为AddTask用于添加新任务,因此它应该只关注新任务而不是完整的任务列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM