ios - 如何在swiftui中使用内部选择器中的切换以及如何基于切换来更改选择器值

标签 ios swift swiftui swift5 combine

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

我尝试实现它,但是它在选择器内部显示了切换,但是我无法基于切换来更改选择器值。

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

    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/

相关文章:

ios - 如何区分 tableViewCell 中的按钮,在 tableView 中重复使用?

ios - Swift - 在基类中使用 'Self' 的类型?

macos - SwiftUI 列表选中行文本颜色

SwiftUI MKMapView UIViewRepresentable - 简单应用程序崩溃。这是 'Metal API' 库错误还是我做了一些明显错误的事情?

ios - CALayer 作为左边框不起作用?

ios - iOS 堆栈 View 中水平居中 View

ios - 如何设置图像从base64字符串的宽度和高度转换?

ios - 如何检查通知的应用程序设置?

ios - 将经历默认参数提升的对象传递给 'va_start'

swift - SwiftUI 中类型 'some View' 的值没有成员 'onDelete'