@state Specifies the state of the binding value, and its attribute modification is officially recommended to use private. (PS: private is not modified here to demonstrate the difference)
import SwiftUI struct FilterView: View { // @Binding var isFavorite: Bool @State var isFavorite = true var body: some View { Toggle(isOn: $isFavorite) { } let buttonTitle = isFavorite ? Text(buttonTitle)}} struct ProductView: View {var titleS: @state private var changeButttile = true var body: some View {Button (action: { changeButtonTtile.toggle() }) { let buttonTitle = changeButtonTtile ? Text(buttonTitle) FilterView(isFavorite: ChangeButttile)}}Copy the code
So here we see that @state var isFavorite = true is modified by State. So when we click on the FilterView switch we can only refresh the current FilterView. Note here: The FilterView(isFavorite: changeButtonTtile) initializes the value of the changeButtonTtile, which is the actual value.
So let’s comment out State and open up the @bingding line
import SwiftUI struct FilterView: View { @Binding var isFavorite: Bool // @State var isFavorite = true var body: some View { Toggle(isOn: $isFavorite) { } let buttonTitle = isFavorite ? Text(buttonTitle)}} struct ProductView: View {var titleS: @state private var changeButttile = true var body: some View {Button (action: { changeButtonTtile.toggle() }) { let buttonTitle = changeButtonTtile ? Text(buttonTitle) FilterView(isFavorite: $changeButttile) //Copy the code
Here we run the code, click the switch, and what do you find? ProductView’s UI has also changed. At this point, FilterView passes in a reference to changeBtnTitle instead of a value. If you change the @Binding modifier in the child view, the parent view will refresh as well.
Next: Understanding Object server object