swift - 当传入视图的 ObservedObject 更新时,SwiftUI 不会更新?
问题描述
我有一个名为 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 中声明的实例
推荐阅读
- matlab - 在 Matlab 中绘制 3d 数据
- c# - NAudio Asio 录制播放和保存(仅限噪音)
- python - Chromedriver没有足够的权限,即使我是这台机器上唯一的管理员
- angular - 垫子表单域颜色变化
- firebase - 使用 Google 的 Firebase 或 Apache Kafka 发送彩信
- classloader - 卸载Java11中类加载器加载的dll
- sql-server - 基于原始表中未使用的列在数据透视后添加计算列
- node.js - NestJS - 错误过滤器内的异步操作
- c# - 使泛型函数中的 T 继承自某些东西
- jquery - 单击链接后,Jquery nav Toogle 类活动不起作用