Bài đăng

Đang hiển thị bài đăng từ tháng 6 5, 2023

Swift UI : Observable, Observed, State and Binding

State and Binding @State and @Binding are used when you need to pass a variable to another view.   struct ContentView : View { @State var showLogo = true var body : some View { VStack { if showLogo { LittleLemonLogo (bindingVariable : $showLogo) } } } } @State to controlling a variable "showLogo" struct LittleLemonLogo : View { @Binding var bindingVariable : Bool var body : some View { VStack { Image ( "littleLemonLogo" ) Button (action : { bindingVariable. toggle () } , label : { Text ( "Toggle Logo Visibility" ) . font (. title2 ) }) } } } A Boolean variable named bindingVariable of type @Binding is created on line 1 . This variable will receive the state of ContentView 's showLogo variable and will be tied to it, meaning, that any ...