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

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…

Contents