Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将数据传递到数组:[struct],该数组馈送到ForEach循环(SwiftUI)_Swift_Xcode_Swiftui - Fatal编程技术网

将数据传递到数组:[struct],该数组馈送到ForEach循环(SwiftUI)

将数据传递到数组:[struct],该数组馈送到ForEach循环(SwiftUI),swift,xcode,swiftui,Swift,Xcode,Swiftui,我使用的是一个经典的数组:[struct]模型,如下所示: struct ContentView: View { @State var showNewCardView = false var MirleftCards: [mirleftCard] = [ mirleftCard(image: Image("beach"), title: "The Beach", description: "The amazing view of the Grande Plag

我使用的是一个经典的数组:[struct]模型,如下所示:

struct ContentView: View {

    @State var showNewCardView = false

    var MirleftCards: [mirleftCard] = [
        mirleftCard(image: Image("beach"), title: "The Beach", description: "The amazing view of the Grande Plage"),
        mirleftCard(image: Image("sea"), title: "The Sea", description: "The amazing view of the sea"),
    ]

    var body: some View {
        NavigationView{
            ScrollView {
                ForEach(MirleftCards, id: \.id) { card in
                    CardView(card_image: card.image, card_title: card.title, card_description: card.description)
                }
           }
      }
}
struct mirleftCards: Identifiable {
      var id = UUID()
      var image: Image
      var title: String
      var description: String
}
原始结构如下所示:

struct ContentView: View {

    @State var showNewCardView = false

    var MirleftCards: [mirleftCard] = [
        mirleftCard(image: Image("beach"), title: "The Beach", description: "The amazing view of the Grande Plage"),
        mirleftCard(image: Image("sea"), title: "The Sea", description: "The amazing view of the sea"),
    ]

    var body: some View {
        NavigationView{
            ScrollView {
                ForEach(MirleftCards, id: \.id) { card in
                    CardView(card_image: card.image, card_title: card.title, card_description: card.description)
                }
           }
      }
}
struct mirleftCards: Identifiable {
      var id = UUID()
      var image: Image
      var title: String
      var description: String
}
我有一个编辑器。工作表,可以让您上传图像并添加标题和说明:

class NewCardData: ObservableObject {
    @Published var title = ""
    @Published var description = ""
}
struct NewCardView: View {

    @ObservedObject var newCardData = NewCardData()
    @State var image:UIImage?

var body: some View {
                if image != nil {
                    Image(uiImage: image!)
                        .resizable()
                        .scaledToFit()
                        .clipShape(RoundedRectangle(cornerRadius: 20))
                        .overlay(TextField("Title", text: $newCardData.title)
                        .overlay(TextField("Description", text: $newCardData.description)
                       }
          }
}
我的问题是,当用户选择了他们的图像,并输入了标题和描述时,我如何能够自动将该数据发送到ForEach循环中的数组


提前谢谢

如前所述,您可以将卡阵列用作
@EnvironmentObject
。 然后,您可以从两个视图访问阵列,因此只需从
newcardwiew
附加它即可

因此,在SceneDelegate中,您需要添加

var MirleftCards: [mirleftCard] = [
    mirleftCard(image: Image("beach"), title: "The Beach", description: "The amazing view of the Grande Plage"),
    mirleftCard(image: Image("sea"), title: "The Sea", description: "The amazing view of the sea"),
]
此外,更改

let contentView = ContentView()
到 让contentView=contentView().environmentObject(MirleftCards)

从现在起,您可以从每个视图访问这个var。加上

@EnvironmentObject var MirleftCards: [mirleftCard]
添加到两个视图中的视图结构,并从图纸中添加新卡。然后,ContentView应该自动更新