Build a Tip Calculator App in iOS 8 Using Swift
- Introducing Swift, Text Fields, Sliders, Outlets, Actions, View Controllers, Event Handling, NSDecimalNumber, NSNumberFormatter and Automatic Reference Counting
3.1 Introduction
The Tip Calculator app (Fig. 3.1(a))—which you test-drove in Section 1.17—calculates and displays possible tips and bill totals for a restaurant bill amount. As you enter each digit of an amount by touching the numeric keypad, the app calculates and displays the tip amount and total bill amount for a 15% tip and a custom tip (Fig. 3.1(b)). You specify the custom tip percentage by moving a Slider’s thumb—this updates the custom tip percentage Labels and displays the custom tip and bill total in the righthand column of yellow Labels below the Slider (Fig. 3.1(b). We chose 18% as the default custom percentage, because many restaurants in the U.S. add this tip percentage for parties of six people or more, but you can easily change this.
Fig. 3.1 | Tip Calculator when the app first loads, then after the user enters the bill amount and changes the custom tip percentage.
First, we’ll overview the technologies used to build the app. Next, you’ll build the app’s UI using Interface Builder. As you’ll see, Interface Builder’s visual tools can be used to connect UI components to the app’s code so that you can manipulate the corresponding UI components programmatically and respond to user interactions with them.
For this app, you’ll write Swift code that responds to user interactions and programmatically updates the UI. You’ll use Swift object-oriented programming capabilities, including objects, classes, inheritance, methods and properties, as well as various data types, operators, control statements and keywords. With our app-driven approach, we’ll present the app’s complete source code and do a detailed code walkthrough, introducing the Swift language features as we encounter them.