-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Example code with explanation
@StateObject
struct ContentView: View {
@StateObject var person = Person()
var body: some View {
VStack {
PersonView(person: person)
PersonEditorView(person: $person)
}
}
}- Overall, this code demonstrates how to share data between different SwiftUI views:
ContentView,PersonView, andPersonEditorView - The
ContentViewowns aPersonobject and passes it to two different subviews. - The
@StateObjectproperty wrapper is used to indicate that thepersonproperty is owned by theContentView. This means that any changes to thepersonobject will cause theContentViewview to be re-rendered.
How to propagate changes from a child view to its parent view
@ Binding
struct PersonEditorView: View {
@Binding var person: Person
var body: some View {
TextField("Enter name", text: $person.name)
}
}- The
PersonEditorViewsubview uses a@Bindingto modify the name property of the Person object, and the changes are automatically reflected in thePersonViewsubview. - The
@Bindingproperty wrapper means that thePersonEditorViewdoes not have its own state, but rather make change to thepersonobject that owned by theContentView
ObservableObject, @published, and @ObservedObject
import SwiftUI
class Person: ObservableObject {
@Published var name: String = "John"
}
struct PersonView: View {
@ObservedObject var person: Person
var body: some View {
Text(person.name)
}
}- The class
Personconform theObservableObjectprotocol - which is a Protocol that you can adopt to create objects that can be observed for changes. In this example, it means an instance of thePersonobject can be observed for changes. - The
@Publishedis a property wrapper that you can use to mark a property as being observable for changes. Since the propertynameis marked as@Published, you are telling Swift that any changes to thenamevalue should be automatically published to any subscribers. - The
@ObservedObjectmeans you subscribe to changes in the instance of anObservableObject. When you mark the propertypersonwith@ObservedObject, SwiftUI will automatically updatePersonViewthat depend on the object's properties whenever the objectpersonchanges.
ObservableObject
- a Protocol that you can adopt to create objects that can be observed for changes.
@ Published
- a Property Wrapper that you can use to mark a property as being observable for changes.
- When you mark a property with
@Published, you tell Swift that any changes to the property's value should be automatically published to any subscribers.
@ObservedObject
-
a Property Wrapper that you can use to subscribe to changes in an instance of an
ObservableObject. -
When you mark a property with
@ObservedObject, SwiftUI will automatically update any views that depend on the object's properties whenever the object changes. -
The
@ObservedObjectproperty wrapper tells Swift to automatically update the view whenever the value of thenameproperty changes.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels