Students will build a reverse polish notation calculator that demonstrates auto layout, stacks, target-actions on UIButtons, programatic views.
Students who complete this project independently are able to:
- build an app without Storyboard
- use and understand auto layout and constraints
- use and understand the target-action design pattern
- use and understand the coordinate gird system
- use and understand the stack data structure
Delete Main.storyboard
, change the Main Interface in the project file, set up the UIWindow
in the AppDelegate
.
- Select
Main.storyboard
, tap delete, and move to trash. - In the project file (the blue icon), change the "Main Interface" to
LaunchScreen
. - In the
AppDelegate
'sapplication:didFinishLauchingWithOptions:
, set up the window. - Programatically change the background of the view controller you set as the root view controller to see if you have set everything up correctly.
Programatically add a UILabel
and 19 UIButton
s to set up the view a user would expect on a calculator. Use NSLayoutContstraints
set up autolayout. See Screen0.png for an example. Please avoid copying the solution code; type your answer yourself.
Run app after each step. Examples of the view appearance are provided at each step. The screen number matches the step number. Do not proceed to next step until you get your desired constraints for current step. Use background colors or the view debugger.
- Add a UILabel that is toward the top of the view and is the width of the screen.
- Add all buttons to the view. You'll need buttons for 0-9, "/", "*", "-", "+", "Enter".
- Constrain the top row of buttons. Don't worry about the width or height of the buttons at this point.
- Constrain the second row of buttons. Don't worry about the width or height of the buttons at this point.
- Constrain the third row of buttons. Don't worry about the width or height of the buttons at this point.
- Constrain the fourth and bottom row of buttons. Don't worry about the width or height of the buttons at this point.
- Constrain the height of all the buttons to one common button's height; set them to be equal. Don't worry about the width at this point.
- Constrain the width of all the buttons (except the 0 button) to one common button's width; set them to be equal. Worry about the width at this point ;).
Create a Stack
class that will hold the numbers that the user has entered into the calculator. You should have methods to pop a number off the stack, push a number on the stack, log the stack, get the count of number of items on the stack.
- Add a private array of floats as a property.
- Add a function called pop that returns a
Float?
This will remove the last object from the array. - Add a function called push that accepts a
Float
. This will add theFloat
to the array. - Add a function called log that will print the array.
- Add a function called count that will return the count of the array.
Add a function to allow the user to enter a number. When they tap on the digit button, they should see their number appear in the display label.
- Add a property to the view controller to hold where the user is in the middle of typing a number.
- Add a function to the view controller that accepts a
UIButton
as a parameter. - Using the buttons title and the property added in step 1, if the user is in the middle of typing a number, you should append the correct digit, else replace the display label with the correct digit and set the user is in the middle of typing a number property to true.
- Add target-actions to each digit button that will call this function you defined in step 2.
Implement a computed property that will return the display label's text as a ````Floatand set the display label's text with a
Float```.
- Add a property called
displayValue
of typeFloat
. - Add a getter function that will return the display label's value. Convert the String of the text to an NSString to use the
.floatValue
property - Add a setter function that will create a String from the
newValue
and set the display label's text. Also set the user is in the middle of typing a number.
Add a property to hold a Stack instance. Add a function that will push the displayValue
on the stack and log the stack.
- Set the user is in the middle of typing a number property to false.
- Push the display value on the stack
- Log the stack
Add a function that takes in a UIButton
as a parameter. Based on the buttons title perform the requested operation on the top two numbers on the stack.
- Make sure the
UIButton
parameter exists. - Check if the user is in the middle of typing a number. If so, call enter. The user should be able to type "5", "Enter", "5", "*" and see 25.0.
- Check to make sure that there are two numbers on the stack. If so, pop two numbers off the stack. Switch on the button title, under each case perform the correct operation on the two numbers and set the
displayValue
to the return value of the correct operation. Call enter to push the result on the stack.
- Make it so the user can't enter multiple zeros for a number.
- Make it display the numbers better after a calculation. (Do we always want that zero after the decimal appearing?)
- Add a square root, sine, cosine, tanget buttons. (These are one parameter operations.)
- Add a display so the user can see previous commands entered.
- Add a clear button to clear above history.
- Add a backspace button to allow user to delete individual digits.
- Build using a different type of autolayout than you previously did. (NSLayoutConstraints, Anchors, StackViews, Visual Format Language, Storyboards)
Please refer to CONTRIBUTING.md.
© DevMountain LLC, 2015. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.