ios - SwiftUI : Picker does not update correctly when changing datasource

标签 ios swift swiftui ios13 picker

我刚刚开始学习 SwiftUI,却陷入了困境!

我试图在更改另一个段的值时更改段样式选择器数据源。但不知怎的,它没有按预期工作!否则我可能编码错误。请问谁能解答一下吗?

这是我的代码:

import SwiftUI

struct ContentView: View {    

@State var selectedType = 0
@State var inputUnit = 0
@State var outputUnit = 1

let arrTypes = ["Temperature", "Length"]

var arrData: [String] {
    switch self.selectedType {
    case 0:
        return ["Celsius", "Fahrenheit", "Kelvin"] //Temperature
    case 1:
        return ["meters", "kilometers", "feet", "yards", "miles"] //Length        
    default:
        return ["Celsius", "Fahrenheit", "Kelvin"]
    }        
}


var body: some View {
    NavigationView{
        Form
        {
            Section(header: Text("Choose type"))
            {
                Picker("Convert", selection: $selectedType) {
                    ForEach(0 ..< 2, id: \.self)
                    { i in
                        Text(self.arrTypes[i])
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
            }

            Section(header: Text("From"))
            {
                Picker("", selection: $inputUnit) {
                    ForEach(0 ..< arrData.count, id: \.self)
                    {
                        Text(self.arrData[$0])
                    }
                }
                .pickerStyle(SegmentedPickerStyle())                    
            }

            Section(header: Text("To"))
            {
                Picker("", selection: $outputUnit) {
                    ForEach(0 ..< arrData.count, id: \.self)
                    {
                        Text(self.arrData[$0])
                    }
                }
                .pickerStyle(SegmentedPickerStyle())
            }                

        }
    }
}
}

当我将段从Length更改回Temperature时,它会以某种方式合并数组。我尝试调试并打印日志中的 arrData 计数,然后它打印正确的结果但不更新 UI!

默认选择第一个片段: enter image description here

更改分割:

enter image description here

将分段更改回第一个分段:

enter image description here

任何帮助或建议将不胜感激。

最佳答案

Nick Polychronakis 在这个分支中解决了这个问题: https://github.com/nickpolychronakis/100DaysOfSwiftUI/tree/master/UnitCoverter

解决方案是将 .id(:identifier:) 添加到您的选择器中,使其具有唯一性。

可观察变量:

@State var unit = 0

主选择器:

Picker("Length", selection: $unit) {
                    ForEach(0 ..< inputUnitTypes.count) {
                        Text("\(self.inputUnitTypes[$0].description)")
                    }
                }
                .pickerStyle(SegmentedPickerStyle())

二级选择器之一,其内容由单位变量确定。

Picker("Length", selection: $inputUnit) {
                        ForEach(0 ..< selected.count) {
                            Text("\(self.selected[$0].description)")
                        }
                    }
                    .id(unit)

关于ios - SwiftUI : Picker does not update correctly when changing datasource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58352798/

相关文章:

ios - 自定义绘制矩形: parameter

ios - 在 prepareForSegue 中使用自定义初始化(带参数)

ios - 如何在 iOS swift 中获取 1 小时前的日期?

ios - 同一部分内单元格之间的可变空间

swift - ScrollView + NavigationView动画故障SwiftUI

ios - Swift 3.0 CloudKit fetchDatabaseChangesCompletionBlock 错误

ios - 将 UnsafePointer<UInt8> 获取到 Int 以将编码的 Int 写入流

swift - 在 SwiftUI 中同时打开多个预览

ios - 如何通过单击按钮在 Swift UI 中从一个 View 导航到另一个 View

ios - CCCrypto解密: exactly one block less