Academy for iOS App Development

Exercise 1:
Tip Calculator

In this exercise, we will create a simple tip calculator for a customer to use in a restaurant. The customer can enter the amount of the bill to be paid, and use a slider to select a percentage from 0 to 100 to use. The app will then display the tip to be paid.

Getting Started

  1. Install Xcode on your Mac, if you haven't already done so. You can find it in the Mac App Store by clicking on this link: Xcode in the App Store.
  2. Launch Xcode.
  3. Click on "Create a new Xcode project".
  4. You should see a window titled, "Choose a template for your new project:". Make sure that "iOS" is highlighted in the tab below the title. Select "App" and then click the "Next" button.
  5. You should see a window titled, "Choose options for your new project:".
  6. The next screen has the same title, "Choose options for your new project", but it's a file explorer that allows you to choose where you want to store your project on your computer. You can choose any location you want, but make sure you put it somewhere you can easily find it. I prefer to always put it in my "Documents" folder, knowing I can always move it somewhere else later when I'm finished working on it. Navigate to your desired location. Leave the "Source Control" checkbox unchecked, and click the "Create" button.
  7. Xcode has now created your project! It should look something like this:
Picture of Storyboard
Figure 1. Ready to start your project!


The Storyboard

We are going to build a user interface that looks like Figure 2.

  1. In the File Navigator pane on the left side, click on Main.storyboard.
  2. Click on the View Controller (the big white space) in the middle of the screen.
  3. Access the Library by clicking on the plus sign at the top of the Xcode window, to the right of where it says "Tip Calculator: Ready". This will cause the Library window to pop up.
  4. Locate the entry "Label" in the menu on the left side of the Library. Add a label object to the View Controller by dragging and dropping it from the Library to the View Controller.
  5. Double-click on the label, and replace the word "Label" with the string, "Enter the amount of the bill:". Hit the Enter key, or click on the white background to finish editing.
  6. Drag and drop the label to the approximate position shown in Figure 1. (It doesn't have to be exact.)
  7. Drag a "Text Field" from the Library to the View Controller and place it in the approximate position shown under the label you just created.
  8. Drag a "Slider" from the Library and place it in the middle of the View Controller.
  9. In a similar fashion, create and place four more labels: "Tax Percentage", "Calculated tax", "0", and "100". Notice that as you change the strings inside the labels, the "names" of the objects in the list of storyboard objects change accordingly.
Picture of Storyboard
Figure 2. The storyboard

Connecting the Storyboard to the Code

In the next step, we need to connect the user interface objects that we just created to variables in the code, so we can refer to their properties. We do this by creating outlets.

A Test Build

Let's build the app to see what it looks like so far, and see if there are any problems that we need to fix before proceeding.

Familiarize yourself with the top bar of your Xcode window. From left to right, you should see:

and then several other things, including the "+" icon for the library.

Adding the Code

First, let's create and initialize some variables. When our tip calculator is launched, let's help the user understand how it works by entering an example value of $50.00 for the bill into the text field. We will also set the tax percentage at 15%, and set the tax percentage at 15%Add a blank line directly under the outlets you created (for readability), and then add the following four lines:

    // Initialize some default values for variables
    var total: Double = 0.0
    var taxPct: Double = 100.0
    var tip: Double = 0.0
        

Next, let's update the user interface slightly. The function viewDidLoad() is automatically called whenever the view controller (the "screen") is loaded into memory. When our tax calculator app is loaded, let's put a value of 50.00 into the text field so the user can see an example of what should be entered. Let's also place the position of the slider to the far right. To do this, we need to modify the code in viewDidLoad():

    override func viewDidLoad() {
        super.viewDidLoad()
        // Assign initial values for bill and tax rate upon startup
        billTextField.text = "50.00"
        taxPctSlider.value = 1
    }
        

Here's what that code does:

Our ViewController.swift file should now look something like this:

Updated code
Figure 8. Updated code

Finally, we need to make the slider work.

Insert the following code between the curly braces "{" and "}".

      total = Double(billTextField.text!)!
      taxPctLabel.text = "Tax Percentage (" + String(format: "%0.2f",taxPctSlider.value*100) + "%)"
      tip = total * Double(taxPctSlider.value)
      taxLabel.text = String(format: "%0.2f", tip)
        

Here's what that code does:

The bottom of your file should look something like this:

Final code
Figure 9. Final code

Complete Build

Build the app again by clicking the "Stop" button, followed by the "Play button", and confirm that the app works! The function taxPercentageChanged gets called automatically any time the user moves the slider, so this causes the values in the tax percentage label and the tax label to be constantly updated. If you move the slider back and forth, you should see the numbers above and below the slider changing.

Confirm that the numbers you get make sense for a bill of $50.00. Then try clicking on the text field and changing the bill to some other number, and confirm that the numbers still make sense when you move the slider around.

Additional Exercises

Figure out how to modify the code to change your app's behavior.