SwiftUI:有条件地显示不同的工作表项目

app*_*sch 3 swiftui

我在尝试如何使用时惨遭失败.sheet(item:content:). 我知道这里和其他平台上有很多信息,但我就是无法让它发挥作用......

\n

这是我想要 \xe2\x80\x93 的视图的抽象,我不知道我在这里做错了什么:

\n
import 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}\n
Run 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修饰符导致当我注释掉它时,视图工作得很好......

\n

Asp*_*eri 5

只需删除闭包返回类型(从返回的内容自动推断),即

    .sheet(item: $showSheet) { sheet in      // << here !!
        Text(sheet.rawValue)
    }
Run Code Online (Sandbox Code Playgroud)