2

我需要使用选择器和切换,并且需要更改基于切换值的选择器值。

我已尝试实现它,但它在选择器内显示切换,但我无法根据切换更改选择器值。

这是我到目前为止所尝试的:

    Picker(selection: $bluetooth.type, label: BluetoothContainer()){
            Toggle(isOn: self.$bluetooth.isBluetoothOn) {
            Text("Bluetooth")
        }

     }
4

1 回答 1

0

这是根据切换状态更新选择器值的内容。

struct ContentView : View {

    @State var selection: String = ""
    @State var isOn = false

    var body: some View {
        VStack {
            NavigationView {
                Form {
                    Section {
                        Toggle(isOn: $isOn) {
                            Text("Bluetooth")
                        }
                        Picker(selection: $selection, label:
                            Text("Picker Name")
                            , content: {
                                Text( isOn ? "Bluetooth is On" : "Bluetooth is Off").tag(0)
                                Text("Value 2").tag(1)
                        })

                    }
                }
            }
        }
    }
}
于 2019-06-27T04:39:23.500 回答