我在尝试如何使用时惨遭失败.sheet(item:content:). 我知道这里和其他平台上有很多信息,但我就是无法让它发挥作用......
这是我想要 \xe2\x80\x93 的视图的抽象,我不知道我在这里做错了什么:
\nimport SwiftUI\n\nenum SheetView: String, Identifiable {\n var id: Self { self }\n case sheetA = "Sheet A"\n case sheetB = "Sheet B"\n}\n\nstruct SheetViewTest: View {\n @State private var showSheet: SheetView? = nil\n \n var body: some View {\n Form {\n Button(action: {\n showSheet = .sheetA\n print("Button \\(showSheet?.rawValue) tapped\xe2\x80\xa6")\n }, label: {\n Text(SheetView.sheetA.rawValue)\n })\n Button(action: {\n showSheet = .sheetB\n print("Button \\(showSheet?.rawValue) tapped\xe2\x80\xa6")\n }, label: {\n Text(SheetView.sheetB.rawValue)\n })\n }\n .sheet(item: $showSheet) { sheet -> View in\n Text(sheet.rawValue)\n }\n }\n}\n\nstruct SheetViewTest_Previews: PreviewProvider {\n static var previews: some View {\n SheetViewTest()\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n我在身体上得到的错误如下:\n Value of protocol type \'View\' cannot conform to \'View\'; only struct/enum/class types can conform to protocols,这是由于.sheet修饰符导致当我注释掉它时,视图工作得很好......
只需删除闭包返回类型(从返回的内容自动推断),即
.sheet(item: $showSheet) { sheet in // << here !!
Text(sheet.rawValue)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
652 次 |
| 最近记录: |