ObservableObject 类是从 ContentView() 和另一个 Swift 类中实例化的。当 Swift 类运行 ObservableObject 类的函数时,它不会更新 ContentView() 的 @ObservedObject。
我知道这是因为我两次实例化 ObservableObject 类。当 ContentView() 不能/不能实例化 Observable 类时,使用 @ObservedObject 的最佳做法是什么?
我还没有找到让 @EnvironmentObject 与 Swift 类一起工作的方法。
我可以使用全局变量并运行 Timer() 来检查对它的更改。但是,这感觉像是一种丑陋的做法?!?
请参阅下面的示例代码。请在设备上运行,以查看打印语句。
import SwiftUI
struct ContentView: View {
@ObservedObject var observedClass: ObservedClass = ObservedClass()
// The callingObservedClass does not exist on the ContentView, but is called
// somewhere in the app with no reference to the ContentView.
// It is included here to better showcase the issue.
let callingObservedClass: CallingObservedClass = CallingObservedClass()
var body: some View {
VStack {
// This Text shall be updated, when
// self.callingObservedClass.increaseObservedClassCount() has been executed.
Text(String(observedClass.count))
Button(action: {
// This updates the count-variable, but as callingObservedClass creates
// a new instance of ObservedClass, the Text(observedClass.count) is not updated.
self.callingObservedClass.increaseObservedClassCount()
}, label: {
Text("Increase")
})
}
}
}
class CallingObservedClass {
let observedClass = ObservedClass()
func increaseObservedClassCount() {
// Returning an Int here to better showcase that count is increased.
// But not in the ObservedClass instance of the ContentView, as the
// Text(observedClass.count) remains at 0.
let printCount = observedClass.increaseCount()
print(printCount)
}
}
class ObservedClass: ObservableObject {
@Published var count: Int = 0
func increaseCount() -> Int {
count = count + 1
return count
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
编辑:我想我的问题是当我无法从 SwiftUI 视图实例化 Swift 类时,如何从 Swift 类获取数据并更新 SwiftUI 视图。