![](/img/trans.png)
[英]SwiftUI - View doesn't update despite using @Published and @ObservedObject
[英]When an ObservedObject passed into a view is updated, the SwiftUI doesn't update?
我有一個名為 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 數組更新時更新..?
對不起大家的困惑,顯然我真的很愚蠢。 我正在創建一個新的 ViewModel() 實例,而不是傳入 ContentView 中聲明的實例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.