不确定我是在做错什么还是错误。我正在运行Xcode 12.1和iOS 14.1。这不是在Xcode 11上发生的。
代码如下:
enum SheetView: Identifiable {
var id: SheetView { self }
case add
}
struct ContentView: View {
var items = ["Book", "Cat", "Dog"]
@State private var sheetView: SheetView?
var body: some View {
NavigationView {
List {
ForEach(items, id: \.self) { item in
Text(item)
}
}
.navigationTitle("Items")
.toolbar {
ToolbarItem(placement: .primaryAction) {
Button(action: {
sheetView = .add
}, label: {
Text("Add")
})
}
}
.sheet(item: $sheetView, onDismiss: { sheetView = nil }) { sheet in
if sheet == .add {
Text("Add")
}
}
}
}
}