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
From the course: Programming for Non-programmers: iOS 17 and Swift 5
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.
Contents
-
-
-
-
-
-
(Locked)
Understanding methods5m 30s
-
(Locked)
Using methods3m 47s
-
(Locked)
Running code when a button is pressed3m 43s
-
(Locked)
Using a method as a variable4m 46s
-
(Locked)
Passing parameters to method variables3m 24s
-
(Locked)
SwiftUI binding5m 21s
-
(Locked)
Using different methods to handle different events3m 2s
-
(Locked)
Challenge: Create methods for the equal button52s
-
(Locked)
Solution: Create methods for the equal button48s
-
(Locked)
-
-
-