【问题标题】:How can I add a `view` type to my identifiable data?如何向我的可识别数据添加“视图”类型?
【发布时间】:2021-03-07 13:16:41
【问题描述】:

我有以下可识别的数据。我想要做的是我想添加viewtype,以便稍后我可以提供一个链接到这些视图的Button。我不知道如何做到这一点。它说它不接受视图类型。

我的目标是创建卡片,每张卡片都包含Datum 值。我想为每个人设置一个Button,将用户带到该特定视图。由于我做了一个ForEach,我希望能够添加每个Datum 的视图。我做了以下但swift不接受并抛出错误。

代码如下:

struct Datum:Identifiable {
    var id = UUID()
    var subtitle:String
    var name:String
    var footnote: String
    var description:String
    var image:String
    var categoryTag:String
    var comingSoon:Bool
    //var modelName: **View**
}

public var categoriesList = ["DL", "TSA", "NLP"]

var ModelsData:[Datum] = [
    Datum(subtitle: "Deep Learning",
         name: "Mask Detection",
         footnote: "An image classificaton model.",
         description: "This model classifies whether a person has a mask        or not. You simply take your camera and then take your face. Then, it will go and scan it. After that, you will see that at the bottom it shows green (means you have a mask) or red (which means you dont have a mask.)",
         image: "MaskDetectionImage",
         categoryTag: "DL",
         comingSoon: false,
         //modelName: MaskDetectionView()),      //I want to add a view
    
    Datum(subtitle: "Deep Leaning",
         name: "Video games",
         footnote: "An image classificaton app",
         description: "This model classifies whether a person has a mask or not. You simply take your camera and then take your face. Then, it will go and scan it. After that, you will see that at the bottom it shows green (means you have a mask) or red (which means you dont have a mask.)",
         image: "latest_1",
         categoryTag: "DL",
         comingSoon: false,
         //modelName: ContentView())] //Another different view

这是图片:

我定义:

错误:

【问题讨论】:

  • 换个方式试试。而不是让 Datum 的实例拥有一个 View 属性;创建您的 SwiftUI 卡片视图以将 Datum 的实例作为属性保存。
  • 你能告诉我怎么做吗?

标签: ios swift uiview swiftui identifiable


【解决方案1】:

这里我做了一个你需要的例子,如果你需要解释,请告诉我。

import SwiftUI


struct Datum {
    var modelName: AnyView
}


struct ContentView: View {
    
    
    var datum: Datum = Datum(modelName: AnyView(Text("Omid").bold()) )
    
    
    var body: some View {

        datum.modelName
     
    }
}

这里:

import SwiftUI

struct ContentView: View {
    
    @State var ArrayOfDatum: [Datum] = ModelsData
    
    
    var body: some View {
        
        
        Divider()
        
        ForEach (0 ..< ArrayOfDatum.count) { index in
            Text(ArrayOfDatum[index].name)
            ArrayOfDatum[index].modelName
            Divider()
        }.font(Font.largeTitle.bold())
        
        
    }
}

struct Datum:Identifiable {
    var id = UUID()
    var subtitle:String
    var name:String
    var footnote: String
    var description:String
    var image:String
    var categoryTag:String
    var comingSoon:Bool
    var modelName: AnyView
}

public var categoriesList = ["DL", "TSA", "NLP"]

var ModelsData: [Datum] = [
    Datum(subtitle: "Deep Learning",
         name: "Mask Detection",
         footnote: "An image classificaton model.",
         description: "This model classifies whether a person has a mask        or not. You simply take your camera and then take your face. Then, it will go and scan it. After that, you will see that at the bottom it shows green (means you have a mask) or red (which means you dont have a mask.)",
         image: "MaskDetectionImage",
         categoryTag: "DL",
         comingSoon: false, modelName: AnyView(Image(systemName: "person")))
         //modelName: MaskDetectionView()),      //I want to add a view
    
    ,Datum(subtitle: "Deep Leaning",
         name: "Video games",
         footnote: "An image classificaton app",
         description: "This model classifies whether a person has a mask or not. You simply take your camera and then take your face. Then, it will go and scan it. After that, you will see that at the bottom it shows green (means you have a mask) or red (which means you dont have a mask.)",
         image: "latest_1",
         categoryTag: "DL",
         comingSoon: false, modelName: AnyView(Image(systemName: "gamecontroller")))
         //modelName: ContentView())] //Another different view
]

【讨论】:

  • 谢谢。我添加了我的视图而不是文本并得到了这个:Static method 'buildBlock' requires that 'modelName' conform to 'View'
  • 您必须添加 .modelName 才能结束尝试并告诉我结果
  • 1.所以我像你一样创建了一个结构。 2.然后我把它给我的可识别 -> var modelName:Datum 3.然后在定义每个项目时,我也添加了它。我已经更新了帖子并添加了图片。请检查一下
  • 你编码错了!你做了两次!让我看看我能不能编辑你的代码。
  • 问题是fullpresentationcover 开辟了一个新的ViewAnyView != View
【解决方案2】:

这将使您了解如何开始构建它以及如何保持 Datum 模型独立于 SwiftUI(或者您想要呈现、保存等)。

struct Datum: Identifiable {
  let name: String
  var id: String { name }
}

struct ContentView: View {
  @State var selectedDatum: Datum?
  let datums: [Datum] = [
    Datum(name: "abc"), Datum(name: "xyz")
  ]
  var body: some View {
    ForEach(datums) { datum in
      Text(datum.name)
        .onTapGesture {
          selectedDatum = datum
        }
    }
    .fullScreenCover(item: $selectedDatum) { datum in
       Color.green
        .overlay(Text(datum.name).font(.headline))
      .onTapGesture {
        selectedDatum = nil
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-21
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多