Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manual update of SwiftUI View

Traditionally, a SwiftUI application will automatically update its view in response to any state changes. This occurs when the @State propertyWrapper detects any updates. I'm curious, is it possible to manually redraw a SwiftUI View, instead of relying on the @State data binding technique ?

like image 242
The Internet Avatar asked Oct 25 '25 01:10

The Internet


1 Answers

You can use an ObservableObject object as model and then call objectWillChange.send() to trigger a refresh

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

class Model: ObservableObject { 
    func reloadView() {
        objectWillChange.send()
    } 
}
like image 195
Sorin Lica Avatar answered Oct 26 '25 16:10

Sorin Lica