我对SwiftUI比较陌生,我正在努力开发我的第一个应用程序.我正在try 使用分段选取器为用户提供在日视图和周视图之间切换的选项.在这些视图的每一个中,都会有特定的用户数据,这些数据应该以图形的形式显示.我遇到的问题是加载数据.我发布了下面的代码,但从我看到的情况来看,问题归结为以下几点:

当加载视图时,它从加载Dayview开始,因为选定的TimeInterval=0.这很好,但是当用户按下分段选取器中的"周"时,数据不会显示.这是由于在从分段选取器运行.onChange()函数之前加载视图的其余部分.由于.onChange是将调用放入viewModel以加载新数据的对象,因此没有数据.如果运行下面的代码,您可以在打印语句中看到这一点.

我本以为视图加载顺序应该是

装载式分段拾取器 如果值已更改,则运行.onChange 加载视图的其余部分 但实际的顺序是

加载分段式拾取器, 加载视图的其余部分(加载图表时此处没有数据!) 如果值已更改,则运行.onChange. 我完全迷路了,所以任何帮助都是最好的!非常感谢!

import SwiftUI 
import OrderedCollections

class ViewModel: ObservableObject{ 
    @Published var testDictionary: OrderedDictionary<String, Int> = ["":0]

    public func daySelected() {
        testDictionary = ["Day View Data": 100]
    }

    public func weekSelected() {
        testDictionary = ["Week View Data": 200]
    }
}


struct ContentView: View {
    @State private var selectedTimeInterval = 0
    @StateObject private var vm = ViewModel()
    
    var body: some View {
        VStack {
            Picker("Selected Date", selection: $selectedTimeInterval) {
                Text("Day").tag(0)
                Text("Week").tag(1)
            }
            .pickerStyle(SegmentedPickerStyle())
            .onChange(of: selectedTimeInterval) { _ in
                let _ = print("In on change")
                //Logic to handle different presses of the selector
                switch selectedTimeInterval {
                case 0:
                    vm.daySelected()
                case 1:
                    vm.weekSelected()
                default:
                    print("Unknown Selected Case")
                }
            }
            switch selectedTimeInterval {
            case 0:
                let _ = print("In view change")
                Day_View()
            case 1:
                let _ = print("In view change")
                Week_View(inputDictionary: vm.testDictionary)
            default:
                Text("Whoops")
            }
        }
    }
}

struct Day_View: View {
    
    var body: some View {
        Text("Day View!")
    }
}

struct Week_View: View {
    @State private var inputDictionary: OrderedDictionary<String,Int>
    
    init(inputDictionary: OrderedDictionary<String,Int>) {
        self.inputDictionary = inputDictionary
    }
    
    var body: some View {
        
        let keys = Array(inputDictionary.keys)
        let values = Array(inputDictionary.values)
        VStack {
            Text(keys[0])
            Text(String(values[0]))
        }
    }
}


struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

推荐答案

在您的WeekView中,更改

@State private var inputDictionary: OrderedDictionary<String,Int>

private let inputDictionary: OrderedDictionary<String,Int>

@State is for the local state of the view. The idea is that you are initing it with initial state and from then on the view itself will change it and cause re-renders. When the WeekView is re-rendered, SwiftUI is ignoring the parameter you pass in至 the init and copying it from the previous WeekView 至 maintain state.

But, you want 至 keep passing in the dictionary from ContentView and cause re-renders from the parent view.

Swift相关问答推荐

如何更新Square Order?

如何在HStack中均匀分布视图?

通常从数组调用的SWIFT静态协议函数

ClosedRange.Swift 中 Int 的索引?

循环字典中的数组

SwiftUI 中的同一个 ForEach 中是否可以有 2 个数组?

如何制作一个在 SceneKit 中投射阴影的透明物体?

在 RealityKit 中查找特定模型的 findEntity(named:) 方法的替代方法?

从 iPhone 中的安全飞地获取真正的随机数?

将内容与工作表顶部而不是中间对齐

SwiftUI Observed Object 在发布值更改时未更新

获取 SwiftUI 中 Binding 的值更改更新

为什么 Swift Tuples 只能在元素个数小于等于 6 的情况下进行比较?

如何在不阻塞 UI 更新的情况下使用 Swift Concurrency 在后台执行 CPU 密集型任务?

如何在 Swift 中复制字典?

调整文本的字体大小以适应 UIButton

使用 phimagemanager 将图像保存到自定义相册?

<<错误类型>> 奇怪的错误

swift中的匿名类

滑动侧边栏菜单 IOS 8 Swift