From the course: Complete Guide to SwiftUI

Unlock this course with a free trial

Join today to access over 24,700 courses taught by industry experts.

Solution: Add a button

Solution: Add a button

(upbeat music) - [Instructor] Okay, there's lots of ways of doing this one, but here's one with a few tricks that I tend to use. First of all, we're going to make a state variable, and we're going to do that over here in a menu item view. So go over here. And above the body, you'll put a state variable, making sure you put private in there. And I'm going to call it Added Item. And my way of doing this will be just to add a flag to do with this. Okay now, inside of this, we're going to now start making our button, and the button is going to go, in this case we got a V stack. I'm going to close up some of my other stacks here so I can see where I am. Here's this stack here, and I'm going to try to put it underneath this stack all the way at the bottom of the V stacks we've got. So it'll always be on the bottom here. And I'll start with button. And we're going to do the action first here. So I'm going to hit return. And inside of here, added item equals true. So all this will do is when…

Contents