SwiftUI @Binding update doesn't refresh view

前端 未结 5 2118
一个人的身影
一个人的身影 2021-02-19 21:31

I feel like I\'m missing something very basic, but this example SwiftUI code will not modify the view (despite the Binding updating) when the button is clicked

Tutorials

5条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-19 21:58

    You need to use @State instead of @Binding.

    • If the UI should update when its value changes, you designate a variable as a @State variable. It is the source of truth.

    • You use @Binding instead of @State, when the view doesn't own this data and its not the source of truth.

    Here is your variable:

    @State var isSelected: Bool
    

提交回复
热议问题