From the course: Programming for Non-programmers: iOS 17 and Swift 5

Unlock this course with a free trial

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

Running code when a button is pressed

Running code when a button is pressed

- [Instructor] And now back to building our calculator. Our next goal is to respond when a button is pressed. So I'm going to go to the CalculatorButton tab, and if that's not open, you can always use the project navigator by clicking this button or hovering over the left edge of the screen, clicking on the folder icon and then single clicking on CalculatorButton. Inside of here, we're going to declare a method that will handle a button press. So below our body declaration, so at the end of line 21 on my code, I'm going to go down a few more lines and I'm going to create the method using the func keyword. Let's call this buttonWasPressed, some parentheses, curly braces. And then we're going to use something called a print statement. And what that's going to do is put some kind of a message in the output window when the app is running. So in there, we need to pass in a string. So in the parentheses, pass in buttonText. The app is clickable now and I can click on the one and nothing…

Contents