首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有选项的SwiftUI选择器在iOS16中没有选择

带有选项的SwiftUI选择器在iOS16中没有选择
EN

Stack Overflow用户
提问于 2022-09-30 02:57:16
回答 2查看 623关注 0票数 1

我使用Picker选项进行无选择,在iOS15中它工作得很好,但是在iOS16中它有一个默认值,如何删除这个默认值,当选择为零时,我不需要将文本显示在Picker行的右边。

代码语言:javascript
复制
struct ContentView: View {
    
    @State private var selection: String?
    let strengths = ["Mild", "Medium", "Mature"]
    
    var body: some View {
        NavigationView {
            List {
                Section {
                    Picker("Strength", selection: $selection) {
                        ForEach(strengths, id: \.self) {
                            Text($0).tag(Optional($0))
                        }
                    }
                }
            }
        }
    }
}

在iOS15中,当选择为零时,不会在选择器行的右侧显示文本。

但是在iOS 16中,相同的代码导致不同的结果,当选择为零时,它有一个默认值。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-09-30 06:36:02

Xcode 14.1 Beta 3日志:"Picker:选择“"nil”无效,并且没有关联的标记,这将提供未定义的结果。

要解决这个日志,您需要添加一个使用nil标记的选项。

代码语言:javascript
复制
struct ContentView: View {

    @State private var selection: String?
    let strengths = ["Mild", "Medium", "Mature"]

    var body: some View {
        NavigationView {
            List {
                Section {
                    Picker("Strength", selection: $selection) {
                        Text("No Option").tag(Optional<String>(nil))
                        ForEach(strengths, id: \.self) {
                            Text($0).tag(Optional($0))
                        }
                    }
                    Text("current selection: \(selection ?? "none")")
                }
            }
        }
    }
}
票数 3
EN

Stack Overflow用户

发布于 2022-10-18 18:05:22

这就是我在iOS 14.0.1中为XCode 16.0所做的事情(为了避免用户对iOS 16.0设备感到烦躁):

代码语言:javascript
复制
let promptText: String = "select" // just a default String
    
//short one for your example
Section {
    Picker("Strength", selection: $selection) {
        if selection == nil { // this will work, since there is no initialization to the optional value in your example
            Text(promptText).tag(Optional<String>(nil)) // is only shown until a selection is made
        }
        ForEach(strengths, id: \.self) {
            Text($0).tag(Optional($0))
        }
    }
}
    
// more universal example
Section {
    Picker("Strength", selection: $selection) {
        if let safeSelection = selection{
            if !strengths.contains(safeSelection){ // does not care about a initialization value as long as it is not part of the collection 'strengths'
                Text(promptText).tag(Optional<String>(nil)) // is only shown until a selection is made
            }
        }else{
            Text(promptText).tag(Optional<String>(nil))
        }
        ForEach(strengths, id: \.self) {
            Text($0).tag(Optional($0))
        }
    }
}
    
// Don't want to see anything if nothing is selected? empty String "" leads to an warning. Go with non visual character like " " or 'Horizontal Tab'. But then you will get an empty row...
Section {
    let charHorizontalTab: String = String(Character(UnicodeScalar(9)))
    Picker("Strength", selection: $selection) {
        if let safeSelection = selection{
            if !strengths.contains(safeSelection){ // does not care about a initialization value as long as it is not part of the collection 'strengths'
                Text(charHorizontalTab).tag(Optional<String>(nil)) // is only shown until a selection is made
            }
        }else{
            Text(charHorizontalTab).tag(Optional<String>(nil))
        }
        ForEach(strengths, id: \.self) {
            Text($0).tag(Optional($0))
        }
    }
}

祝你好运,找到适合你的解决方案。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73903569

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档