The add and subtract buttons of the calculator are vital to the calcuator functioning properly. See how to create the logic for those buttons in this video.
- [Instructor] Our next step is to handle what happens when you press the add or subtract button. We're not going to be able to see the total in the calculator just yet, but we're going to handle didPressAdd and didPressSubtract. So let's scroll up and find didPressPlus and didPressSubtract and what we're going to do in here is just called change mode. And we're going to change the mode to addition and subtraction, respectively. So change modes, we'll pass in .addition and then for subtract, we'll pass in changeModes.subtraction.
Now let's scroll down to the change modes method. It's all the way at the bottom, and it's important to think about how change modes is actually going to work. When we change the mode of the calculator, what we're saying is, we want to make sure that there was an existing number pressed into the calculator. So if there wasn't anything already pressed into the calculator, then we don't care about pressing the plus button or the minus button. So, that's the first thing that we need to do, make sure there's already existing information in the calculator.
Then what change modes is going to do, so you type in seven, and then you type a plus and we're going to keep seven on the screen. And then we're going to say, last button was mode is true, so we leave that seven on the screen until the user presses another numeric button. So that's how we handle changing the calculator modes in addition and subtraction. So, we're going to use the variable SavedNum to save the previous number in the calculator.
So we're going to check to see and hear if savedNum was equal to zero. And if it's equal to zero, we're just going to return out of this method, so stop running any more code in this method and quit out of it. Then what we're going to do is set the current mode equal to the newMode passed in through this method. Finally, we set lastButtonWasMode equal to true. Now, with that set up, we're going to need to do some management with the other methods that we have.
So we're going to scroll up, and inside of didPressNumber, we want to check to see if the last button was mode. So, we'll create an if statement in there, right below where we set the string value. So if curly braces, and in the parentheses of the if statement, check to see if lastButtonWasMode, and if it was, we're going to set it to false. So lastButtonWasMode equals false. It's kind of like that switch we made earlier in the course. If it's true, we just set it to false.
Meaning, the last button was pressed when we pressed a number it was actually a number and it wasn't the mode anymore. And then we reset the label string. So labelString, so labelString equals string zero. So we're saying, if you type in a number, press the plus button, that number stays on the screen and then when you type another number, then we replace that with the new number and we want to save the old number. So let's go inside of updateText.
And inside of here, below the guard statement, so a user has pressed a button, we're updating the text, we want to check to see if the current mode, so, an if statement with parentheses and curly braces, if currentMode is equal to .not_set, so we haven't pressed the addition or subtraction button yet, then what we want to do is save the number. So savedNum = labelInt, so whatever is inside of the label.
So again, we're not yet showing the total in the calculator, we're just setting up what happens when you press the addition button or the subtraction button. So, what we should now see if we test this, and I'll change to an iPhone 8 and run it in the simulator, is that if we type a number and then we hit the plus button or the minus button and type more numbers, we should see the calculator sort of reset when we do that. So five plus six, and then it shows that's six.
So we can type minus and then three, and then the calculator updates as well.
- Installing Xcode
- Creating an Xcode project
- Understanding variables
- Connecting visual objects to variables
- Understanding methods
- Connecting a button to a method
- Using conditional statements
- Setting up the user interface (UI)
- Building a complete iOS app