1
A Simple iOS Application

In this chapter, you are going to write an iOS application named Quiz. This application will show a question and then reveal the answer when the user taps a button. Tapping another button will show the user a new question (Figure 1.1).

Figure 1.1  Your first application: Quiz

Screenshot of an iOS Quiz Application, which has a text area filled with question, followed by a “Next Question” button, a text area filled with question marks, and a “Show Answer” button.

When you are writing an iOS application, you must answer two basic questions:

  • How do I get my objects created and configured properly? (Example: I want a button here that says Next Question.)

  • How do I make my app respond to user interaction? (Example: When the user taps the button, I want this piece of code to be executed.)

Most of this book is dedicated to answering these questions.

As you go through this first chapter, you will probably not understand everything that you are doing, and you may feel ridiculous just going through the motions. But going through the motions is enough for now. Mimicry is a powerful form of learning; it is how you learned to speak, and it is how you will start iOS programming. As you become more capable, you will experiment and challenge yourself to do creative things on the platform. For now, go ahead and do what we show you. The details will be explained in later chapters.

Creating an Xcode Project

Open Xcode and, from the File menu, select NewProject.... (If Xcode opens to a welcome screen, select Create a new Xcode project.)

A new workspace window will appear and a sheet will slide down from its toolbar. At the top, find the iOS section and then the Application area (Figure 1.2). You are offered several application templates to choose from. Select Single View Application.

Figure 1.2  Creating a project

Screenshot of the Xcode IDE’s Choose a template for your new project dialog.

This book was created for Xcode 8.1. The names of these templates may change with new Xcode releases. If you do not see a Single View Application template, use the simplest-sounding template. You can also visit the Big Nerd Ranch forum for this book at forums.bignerdranch.com for help working with newer versions of Xcode.

Click Next and, in the next sheet, enter Quiz for the Product Name (Figure 1.3). The organization name and identifier are required to continue. You can use Big Nerd Ranch or any organization name you would like. For the organization identifier, you can use com.bignerdranch or com.yourcompanynamehere.

From the Language pop-up menu, choose Swift, and from the Devices pop-up menu, choose Universal. Make sure that the Use Core Data checkbox is unchecked.

Figure 1.3  Configuring a new project

Screenshot of the Xcode IDE “Choose options for your new project” dialog.

Click Next and, in the final sheet, save the project in the directory where you plan to store the exercises in this book. Click Create to create the Quiz project.

Your new project opens in the Xcode workspace window (Figure 1.4).

Figure 1.4  Xcode workspace window

Screenshot of the Xcode workspace window with its panels labeled.

The lefthand side of the workspace window is the navigator area. This area displays different navigators – tools that show you different parts of your project. You can open a navigator by selecting one of the icons in the navigator selector, which is the bar just above the navigator area.

The navigator currently open is the project navigator. The project navigator shows you the files that make up a project (Figure 1.5). You can select one of these files to open it in the editor area to the right of the navigator area.

The files in the project navigator can be grouped into folders to help you organize your project. A few groups have been created by the template for you. You can rename them, if you want, or add new ones. The groups are purely for the organization of files and do not correlate to the filesystem in any way.

Figure 1.5  Quiz application’s files in the project navigator

Screenshot showing the project navigator which lists project files and folders for the Quiz Project. The folders are Quiz, QuizTests, QuizUITests, and Products.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset