0

我有一个名为 SearchResults 的视图。每当传入的 ObservedObject 发生更改时,它都应该更新,但是 View 没有使用新值更新,我不知道为什么。

import SwiftUI

struct SearchResults: View {
    @ObservedObject var VModel: ViewModel
    var body: some view {
        List {
            ForEach(self.VModel.searchResults, id: \.self) { result in 
                Text(result)
            }
        }
    }
}


class ViewModel: ObservableObject {
    @Published var searchResults: [String] = []

    func findResults() {
        //Do the update to 'searchResults' here
        //This function is called at another point in the code in a pretty big file...I think it might make it more confusing to have like a couple hundred more lines of code in here
    }
}

旁注:VModel 有一个类型为 [String] 的数组,称为 searchResults,我认为这个视图应该在 searchResults 数组更新时更新..?

4

1 回答 1

0

对不起大家的困惑,显然我真的很愚蠢。我正在创建一个新的 ViewModel() 实例,而不是传入 ContentView 中声明的实例

于 2020-05-26T03:03:06.417 回答