Join Todd Perkins for an in-depth discussion in this video Setting the calculator to add or subtract, part of Programming for Non-Programmers with iOS 8.
- Now we're going to handle what happens when you tap the plus or minus buttons. Inside of those buttons, which we're going to call mode buttons, we're going to run the set mode method. That way, setMode can support both the plus and the minus button. So inset of tappedPlus, we're going to call self.setMode . Then we'll send in for the parameter, a one. For tapped minus, we're going to do the same thing, except for, instead of one, we'll send in negative one.
If mode is one, that means we're working with the plus operator, and if mode is negative one, then we're working with the minus operator. Let's handle setMode. Inset of setMode, the first thing we're going to do, is make sure the user actually tapped some numeric buttons before pressing setMode. So we're going to check to see if total is equal to zero. So we'll put that inside of an if statement, with the parentheses and the curly braces and everything.
If total is zero, that means the user hasn't tapped any numeric buttons. If that's the case, we don't want to run any more code inside of setMode, so we'll call the command that does that, which is return. Below the if statement, we're going to set the value of our mode variable equal to m. Remember that m is the parameter that is passed into setMode. Again, that's what's going to be one, if they tap the plus button, and negative one, if they tap the minus button.
So mode should, here, either be negative one or positive one. Then we want to set the value of lastButtonWasMode. Because, of course, the last button was mode when this method is called. Because it's triggered by either the plus or the minus button. So we'll set this to true, and then on the next line, we're going to set the total to the value that's inside of the label, or the value string. Because both of those values should be the same. So, total, which is an integer, equals valueString, and remember to convert a string to an integer, we use .toint .
Remember, since int has that question mark after it, it's an optional value, so we need to unwrap the value, using an exclamation point. In addition to handling setMode here, we need to handle the mode button in another place. If we scroll up into tappedNumber, we want to check to see if the last button pressed was the mode button. The reason why we want to do that, is because, at this point, we're going to start working with two sets of numbers.
The set of numbers the user presses before pressing plus or minus, and the set of numbers the user presses, after pressing plus or minus. So, right below the existing if statement, we're going to create a new if statement that checks to see if last button was mode. So, if the last button pressed was plus or minus, before pressing the number that was just pressed, that triggered this method, we're going to set lastButtonWasMode equal to false. Then we're going to set the value string to be an empty string, so just two quotes.
That way, the user types in the first set of numbers, presses the plus button, for example, and then when they press a number again, the text field is cleared and replaced with whatever new number the user pressed. And finally, the value that sets the total, here, is no longer sufficient. That's because we want to have the total save only the first set of numbers that are pressed. To do that, we need to wrap this line of code, on line 45 in my code, inside of an if statement.
So, we're only going to set total to the number in the value string, if total is equal to zero. So, if, in parenthesis total, is equal to zero, then we're going to put an open curly brace above total = valueSting.toInt , and then below it, we'll put a closed curly brace. Now, the reason for this is, when we tap the mode button, we want to save the total. So we save that numeric value that's in the value string. Then as we press numbers after we press plus or minus, we don't want to mess with total.
We want to save that, so that we can add or subtract from it when the user presses the equals button. So, if we test the app in the simulator, the only thing you're going to notice, is that after you tap the first set of numbers, and then press either the plus or minus button, you can start tapping the second set of numbers, and those replace the first set. So, with all of this set up, we're ready to start finishing the application by setting what happens when we press the equals button.
If you find you'd like to learn more, see iOS App Development Essential Training, Foundations of Programming: Fundamentals, or any of the other programming courses in our library.
- Installing Xcode
- Creating an Xcode project
- Configuring the iOS Simulator
- Understanding variables
- Connecting visual objects to variables
- Understanding functions, methods, and selectors
- Connecting a button to a method
- Using conditional statements
- Setting up the user interface
- Connecting code elements to build an app