我需要使用选择器和切换,并且基于切换值的选择器值需要更改。
我尝试实现它,但是它在选择器内部显示了切换,但是我无法基于切换来更改选择器值。
到目前为止,这是我尝试过的:
Picker(selection: $bluetooth.type, label: BluetoothContainer()){
Toggle(isOn: self.$bluetooth.isBluetoothOn) {
Text("Bluetooth")
}
}
最佳答案
这是根据切换状态更新选择器值的工具。
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)
})
}
}
}
}
}
}
关于ios - 如何在swiftui中使用内部选择器中的切换以及如何基于切换来更改选择器值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765437/