简体   繁体   English

子视图中的 @State 和 @Published 属性,SwiftUI

[英]@State and @Published properties in child views, SwiftUI

In MyController , I have the following property which is updated in other methods that are called:MyController 中,我有以下属性,该属性在其他调用的方法中更新:

@Published public var data = [Glucose]()

I also have a function, which limits this Published property by a given limit :我还有一个函数,它通过给定的limit来限制这个Published属性:

public func latestReadings(limit: Int = 5) -> [Glucose] {
        // return latests results
}

In a SwiftUI View, I consume this data by the following, which works fine and updates when MyController 's data changes:在 SwiftUI 视图中,我通过以下方式使用此数据,它可以正常工作并在MyControllerdata更改时更新:

    @EnvironmentObject var data: MyController

    var body: Some View {
        ForEach(self.data.latestReadings(limit: 11), id: \.self) {
            /// Display Text etc.
        }
    }

But, I want to call the following here, which converts the Glucose readings into a DataPoint array which the Chart consumes:但是,我想在这里调用以下代码,它将Glucose读数转换为Chart使用的DataPoint数组:

Chart(
    data: self.data.latestReadings(limit: 37),
    formattedBy: { (readings) -> [DataPoint] in
        var result = [DataPoint]()
        var i = 0
        for reading in readings {
            result.append(DataPoint(x: Double(i), y: reading.mmol))
                i += 1
            }
            return result
        }
    )

...Which refers to another SwiftUI View defined as: ...指的是另一个定义为的 SwiftUI 视图:

struct Chart: View {
    // Properties
    @State var data: [DataPoint] // I asusme this should be @State
    var opt: ChartOptions
    
    // Formatters
    private var fmt: Formatting = Formatting.shared
    
    // Init
    public init(data: [Glucose], formattedBy:ChartDataFormatter) {
        _data = State(wrappedValue: formattedBy(data)) // Again I assume this is probably wrong..
    }

    ...draw views etc.
}

This all works on the first time the Chart is drawn, but the data property on the Chart view doesn't re-draw as the MyController data property changes.这一切都适用于第一次绘制Chart ,但Chart视图上的data属性不会随着MyController data属性的更改而重新绘制。 I assume I'm doing something wrong with state and observing changes here?我假设我在状态方面做错了并观察了这里的变化?

If I understood your workflow correctly you don't need state wrapper in Chart, because it prevents value update... so try without it, like如果我正确理解了您的工作流程,您就不需要 Chart 中的状态包装器,因为它会阻止值更新……所以尝试不使用它,例如

struct Chart: View {
    // Properties
    var data: [DataPoint]

    // ... 
    
    // Init
    public init(data: [Glucose], formattedBy:ChartDataFormatter) {
        self.data = formattedBy(data)
    }

    // ...

@State breaks the connection with your Controller. @State断开与您的控制器的连接。 Per the documentation @State should always be private .根据文档@State应该始终是private

Pass the data using @EnvironmentObject and manipulate it within the view or in the Controller .使用@EnvironmentObject传递data并在视图或Controller操作它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM