【问题标题】:Passing List between Views in SwiftUi在 SwiftUi 中的视图之间传递列表
【发布时间】:2021-06-05 17:26:00
【问题描述】:

我正在自己制作一个 ToDo 列表应用程序以尝试熟悉 iOS 开发,但我遇到了一个问题:

我有一个单独的View 链接以使用TextField 输入新任务。这是该文件的代码:

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()
    }
}

所以我需要将输入的任务变量插入到数组中,以便在我的主ContentView 文件的列表中显示。

这是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()
    }
  }
}

我需要一个函数来获取在TextField 中输入的任务,并将其传递回ContentView 的数组中,以便在List 中为用户显示

-感谢您的帮助

【问题讨论】:

    标签: ios swiftui swiftui-view


    【解决方案1】:

    当用户点击Add Task 时,您可以在AddTask 中添加一个闭包属性作为回调。就像这样:

    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()
        }
      }
    
    }
    

    然后,在ContentView

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

    @jnpdx 通过将taskList 的Binding 传递给AddTask 提供了一个很好的解决方案。但我认为AddTask 用于添加新任务,因此它应该只关注新任务而不是完整的任务列表。

    【讨论】:

    • 这是一个很好的解决方案,我同意您关于视图焦点的观点。但是,我可能不会包含 onAddTask 的默认值,因为没有它,视图将毫无用处(尽管我理解它避免了在预览中传递空闭包的问题)。
    • @jnpdx 你是对的,onAddTask 的默认值是不需要的。
    • @jctaoo 这看起来不错,但我收到了一个错误,因为没有将参数传递到 AddTask 文件中的预览提供程序中:“调用中的参数 'onAddTask' 缺少参数”
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    • 1970-01-01
    相关资源
    最近更新 更多