SwiftUI Picker未显示选定的值

huangapple go评论61阅读模式
英文:

SwiftUI Picker not showing selected value

问题

以下是翻译好的部分:

我知道关于这个问题还有其他问题,但它们都没有回答这个问题。

有两种稍微不同的情况导致相同的行为:使用枚举作为 Picker 选项,和使用数组。应用程序构建成功,没有错误或警告。

我尝试了 Picker 代码的几种不同变体,但都没有成功。

这是使用数组的 Picker:

Text("Track")
Picker("", selection: $track) {
    ForEach(tracks, id: \.self) { t in
        Text(t.name).tag(t)
    }
}
.labelsHidden()

这是数组:

let tracks = [Track(name: "Portimao", length: 4.692, downforce: Rating.High, grip: Rating.Low, wear: Rating.High, fuel: Rating.Medium, pitTime: 15.5)]

另一个很相似:

Text("Tyre")
Picker("", selection: $tyre) {
    ForEach(TyreType.allCases, id: \.self) { tyre in
        Text(String(describing: tyre)).tag(tyre)
    }
}
.labelsHidden()
enum TyreType: CaseIterable, Identifiable {
    var id: TyreType {self}
    case Wet
    case ExtraSoft
    case Soft
    case Medium
    case Hard
}

当我尝试使用任何一个 Picker 选择选项时,所有选项都会显示并可选择,但当选择后,Picker 又变为空白。

.tag() 和 .self 对行为没有影响。

英文:

I am aware there are other questions about this, but none of them answer the question.

There are two slightly different cases that are resulting in the same behaviour: using an enum for the Picker options, and using an array. The app builds successfully with no errors or warnings.

I have tried several different variations of the Picker code but none work.

Here is the Picker using the array:

Text("Track")
                    Picker("", selection: $track) {
                        ForEach(tracks, id: \.self) { t in
                            Text(t.name).tag(t)
                            }
                    }
                    .labelsHidden()

and here is the array:

let tracks = [Track(name: "Portimao", length: 4.692, downforce: Rating.High, grip: Rating.Low, wear: Rating.High, fuel: Rating.Medium, pitTime: 15.5)]

The other one is very similar:

Text("Tyre")
                    Picker("", selection: $tyre) {
                        ForEach(TyreType.allCases, id: \.self) { tyre in
                            Text(String(describing: tyre)).tag(tyre)
                            }
                    }
                    .labelsHidden()
enum TyreType: CaseIterable, Identifiable {
    var id: TyreType {self}
    case Wet
    case ExtraSoft
    case Soft
    case Medium
    case Hard
}

When I try to select an option with either of the Pickers, all the options are shown and selectable, but when it is selected the Picker goes blank again.

.tag() and .self make no difference to the behaviour.

答案1

得分: 0

以下是您要翻译的代码部分:

这是一些示例代码,展示了如何使用您在问题中展示的选择器。

请注意,选择器的选择变量需要与您在选择器中使用的 `.tag()` 的类型相同。

// 用于测试
struct Track: Hashable {
    var name: String
    var length: Double
    //....
}

struct ContentView: View {
    // 用于测试
    let tracks = [Track(name: "Portimao", length: 4.692),
                  Track(name: "aaa", length: 123),
                  Track(name: "bbb", length: 456),
                  Track(name: "ccc", length: 789)]
    
    @State var track: Track = Track(name: "Portimao", length: 4.692) 
    @State var tyre: TyreType = TyreType.ExtraSoft
    
    var body: some View {
        
        Text("所选赛道: \(track.name)")
        Picker("", selection: $track) {
            ForEach(tracks, id: \.self) { t in
                Text(t.name).tag(t)
            }
        }.labelsHidden()
        
        Text("所选轮胎: \(tyre.rawValue)")
        Picker("", selection: $tyre) {
            ForEach(TyreType.allCases, id: \.self) { tyre in
                Text(String(describing: tyre)).tag(tyre)
            }
        }.labelsHidden()
    }
}

enum TyreType: String, CaseIterable, Identifiable {
    var id: TyreType {self}
    case Wet
    case ExtraSoft
    case Soft
    case Medium
    case Hard
}

请注意,我已经将 HTML 实体字符(如 ")转换为相应的引号。

英文:

Here is some example code that shows how you can use the Pickers you show in your question.

Note, the Picker selection var, need to be of the same type as the .tag() you use
in the Picker.

// for testing
struct Track: Hashable {
    var name: String
    var length: Double
    //....
}

struct ContentView: View {
    // for testing
    let tracks = [Track(name: "Portimao", length: 4.692),
                  Track(name: "aaa", length: 123),
                  Track(name: "bbb", length: 456),
                  Track(name: "ccc", length: 789)]
    
    @State var track: Track = Track(name: "Portimao", length: 4.692) 
    @State var tyre: TyreType = TyreType.ExtraSoft
    
    var body: some View {
        
        Text("Selected track: \(track.name)")
        Picker("", selection: $track) {
            ForEach(tracks, id: \.self) { t in
                Text(t.name).tag(t)
            }
        }.labelsHidden()
        
        Text("Selected tyre: \(tyre.rawValue)")
        Picker("", selection: $tyre) {
            ForEach(TyreType.allCases, id: \.self) { tyre in
                Text(String(describing: tyre)).tag(tyre)
            }
        }.labelsHidden()
    }
}

enum TyreType: String, CaseIterable, Identifiable {
    var id: TyreType {self}
    case Wet
    case ExtraSoft
    case Soft
    case Medium
    case Hard
}

huangapple
  • 本文由 发表于 2023年6月29日 09:59:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定