简体   繁体   English

SwiftUI手动更新查看

[英]Manual update of SwiftUI View

Traditionally, a SwiftUI application will automatically update its view in response to any state changes.传统上,SwiftUI 应用程序将自动更新其视图以响应任何 state 更改。 This occurs when the @State propertyWrapper detects any updates.当@State propertyWrapper 检测到任何更新时会发生这种情况。 I'm curious, is it possible to manually redraw a SwiftUI View, instead of relying on the @State data binding technique?我很好奇,是否可以手动重绘 SwiftUI 视图,而不是依赖@State 数据绑定技术?

You can use an ObservableObject object as model and then call objectWillChange.send() to trigger a refresh您可以使用ObservableObject object 作为 model 然后调用objectWillChange.send()来触发刷新

struct MyView : View {
    @ObservedObject var model: Model
    var body: some View { .. }
}

class Model: ObservableObject { 
    func reloadView() {
        objectWillChange.send()
    } 
}

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

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