在工作表内的表单中使用选取器不起作用

时间:2020-08-25 06:51:16

标签: swift swiftui ios14 xcode12

我需要在工作表内的表单中使用选择器。但这是行不通的。如果单击选择器,则不会进入元素选择。

struct ContentView: View {
    @State private var showSheet = false
    
    var body: some View {
        Button("Sheet", action: {
            showSheet.toggle()
        })
        .sheet(isPresented: $showSheet, content: {
            SecondView()
        })
    }
}

struct SecondView: View {
    var body: some View {
        Form {
            Picker(selection: .constant(1), label: Text("Picker")) {
                Text("1").tag(1)
                Text("2").tag(2)
            }
        }
    }
}

这里有什么建议吗?

1 个答案:

答案 0 :(得分:1)

它需要NavigationView,即

    .sheet(isPresented: $showSheet, content: {
        NavigationView {
           SecondView()
        }
    })

替代方法是将Form嵌入NavigationView中的SecondView中。