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.
Using different methods to handle different events
From the course: Programming for Non-programmers: iOS 17 and Swift 5
Using different methods to handle different events
- [Instructor] Our next step is to create methods for these other buttons. So we're going to look at the plus, minus, and multiplication buttons here, and also the clear button. So what we want to do is create a method that handles all three of these buttons. We'll call that modeWasPressed for the reason that clicking on one of these changes the mode of the calculator to addition mode, subtraction mode, et cetera. And then we'll create a clearWasPressed method for when you hit the clear button. And all we're going to do now is put print statements in those methods. So let's go over to ButtonGrid. And below numberWasPressed, I'm going to create a new method using the func keyword called modeWasPressed. And just like the other one, it's going to receive a button that is of the type CalculatorButton. So just like that method numberWasPressed right above in the code. So we have modeWasPressed, and in there, I'm going to print the string, mode was pressed, and then I'm going to copy and…
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)
-
-
-