![](/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.