From the course: SwiftUI Essential Training

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

State variables and buttons

State variables and buttons

- [Speaker] With that theory behind you, the first and easiest to understand wrapper is the at state wrapper, as I used in the example. A common place to use one is with a button and we'll set up a button to toggle between order view and menu view. So if you go over to content view in our code we already have this thing called show orders and that's been switching between the two views. We've been doing it manually, but we can actually make it a state variable to make it more automatic. So I'm going to do that at state Var and we're always going to try to make this private. State should never be changed externally. And adding private is a good practice to assure that. Now we'll need some way to change it. And an easy way is a button. Now the basic button in SwiftUI consists of a string and an action. So I'm going to go right below header view here, just below its shadow. And I'm going to put in a button and the object is…

Contents