Your First iOS & SwiftUI App: An App from Scratch

Feb 13 2023 · Swift 5.7, iOS 16, Xcode 14

Part 2: SwiftUI Data

12. Buttons & Actions

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Introduction Next episode: 13. SwiftUI State

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

It's time to add some interactivity to the app. Let's start by making the button simply print out a text message to the console when you tap it. If you recall when we added the button into Bullseye we just put an open and close curly brace here for the action, which means when the button is tapped, run no code at all. So we want to modify this to run a little bit of code. We've got an open line already, so right here type, print, all lowercase and open and close parentheses. And then inside here we can put whatever we want to print out in quotes. So I'm going to do open and close quote. And inside here we'll just write Hello SwiftUI. We are going to have to try this out in the simulator because currently there's not an easy or reliable way to see the result of print statements from interacting with the canvas. So I'll close up the canvas with the adjust editor options button at the top right and uncheck canvas. And now we could click the play button in the upper right but instead I'm going to hit Command R, which is a shortcut to run the app in the simulator. Once it's running, I'm going to put it next to my full screen Xcode, just so we can look at both at the same time. You don't have to do this if you don't want to. Your Mac might be set up differently than mine but to do this, I am swiping up with four fingers on my track pad, then clicking and dragging the simulator over to the right of Xcode. Now go ahead and tap the hit me button. If you don't see the console pop up automatically you can click this button in the lower right here, which expands it. And you'll notice now here in Xcode we see Hello SwiftUI printed out in this area at the bottom called the console. I can tap hit me multiple times and each time I click the button I see this printed out. To hide the debugger console again, you can use that button or if you're a keyboard shortcut person. this one is Shift + Command + Y. Print is an example of a function. A function is like a method except that it's not attached to any instance. Not being attached to a class or struct means that you can use it without having to name an instance first. In Swift, anytime you see a name that starts with a lowercase letter that's immediately followed by parentheses such as font, weight, or print you're probably looking at the name of a function or method. The difference is that methods are preceded by the instance that you're calling the method on. While functions don't belong to any instance and simply appear on their own, the print function takes some text and then prints it to X Codes console, which is the pane in the lower right of Xcode that we saw earlier. You can think of print as a developer's best friend. It's very useful as a debugging tool which means its purpose is to help you figure out what's going on in your program and to find the cause of any bugs that you may have. You only see the results in Xcode while you're developing the app. If you take your app and ship it to the app store print has no effect. It's there just to help you out while you're developing. As you continue programming, you'll find yourself using print as an indicator to check that specific pieces of code are being executed or to check on some value that your program has stored. In the program you just wrote. You're using print to make sure that you run code when you tap the button. The fact that pressing hit me in your app causes print to print Hello SwiftUI in the console is a good sign. It means that you've proven you can make something happen when the button is pressed. So congratulations you've just written some interactive code but you're not done yet. Since print is a debugging tool users never see the result from their point of view. Pressing Hit Me still doesn't do anything. You still have to make the button provide a response that the user can see. And in order to do that we need to go over the concept of SwiftUI State.