在 SwiftUI 中隐藏 Picker 的选定值

发布于 2025-01-11 08:14:17 字数 222 浏览 6 评论 0原文

我在表单中有一个选择器,但我想仅在该表单内的另一个文本字段中显示选择器的选定值,而不是在选择器中。

隐藏所选内容选择器中的值

是否可以隐藏选择器中选定的值?

I have a Picker within a Form but I want to show the selected value of the Picker only in another TextField within this Form and not in the Picker.

Hide selected value in Picker

Is there a possibility to hide the selected value in a Picker?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蝶…霜飞 2025-01-18 08:14:17

您无法使用 Picker 执行此操作,但可以使用 NavigationLink 作为解决方法:

struct ContentView: View {
    
    @State private var input = ""
    @State private var active = false
    
    var body: some View {
        NavigationView {
            Form {
                NavigationLink("Select", isActive: $active) {
                    List {
                        Button("Item 1") {
                            input = "Item 1"
                            active = false
                        }
                        Button("Item 2") {
                            input = "Item 2"
                            active = false
                        }
                        Button("Item 3") {
                            input = "Item 3"
                            active = false
                        }
                    }
                }
                
                TextField("", text: $input)
            }
        }
    }
}

You can't do this with a Picker but you could use NavigationLink as a work-around:

struct ContentView: View {
    
    @State private var input = ""
    @State private var active = false
    
    var body: some View {
        NavigationView {
            Form {
                NavigationLink("Select", isActive: $active) {
                    List {
                        Button("Item 1") {
                            input = "Item 1"
                            active = false
                        }
                        Button("Item 2") {
                            input = "Item 2"
                            active = false
                        }
                        Button("Item 3") {
                            input = "Item 3"
                            active = false
                        }
                    }
                }
                
                TextField("", text: $input)
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文