Blast-Off!
The first program you’ll attempt is a basic, generic Hello World app. This chapter follows precisely the method I have found, through experience, to work very well when teaching this subject. I use this simple Hello World app to introduce students to critical skill sets they’ll use over and over again. As is the case with my own students, by the time you finish this chapter, you’ll know how to run your first app in the three different ways described in this chapter:
These three ways of running apps are critical because these are the most common ways that programmers in the real world code Xcode. Your first adventure with this new set of tools will be saying “Hello” to the world from the Single View Application template in Xcode.
Note Besides the information I present here in this book, including screenshots, I also offer screencasts of me going through each of the examples in this book. Downloading those will help you get through this book, as will lecture notes, third-party resources, and pertinent YouTube videos—all of which can be accessed by clicking the blue iOS 6 icon on the top bar at www.rorylewis.com.
Running your App on the iPhone Simulator
In this first example, you’re going to click a button, and text will appear above it that says “Hello World!”
Figure 2-1. Click the Xcode icon in your Dock to open it. You’ll see the “Welcome to Xcode” screen
Figure 2-2. Name the project helloWorld_01 and use your name or company name for the Company Identifier. For Device, select iPhone
Note My Single View Application template icon was highlighted by default; yours may not be. Regardless, click it and save the new project to your desktop as helloWorld_01.
Figure 2-3. Click helloWorld_01ViewController.xib to open the Interface Builder
As shown in Figure 2-3, double-click ViewController.xibto open the file, located in the manila-colored helloWorld_01 folder inside the blue Xcode folder at the top of the Navigator area of the Xcode environment.
Note There is a slight possibility that your Navigation pane, which bears the folders seen in Figure 2-3, is closed. This is not a problem. To open your Utility area, go to the upper left of the workspace window, which includes inspectors and libraries. Use the View Selector in the toolbar to open and close the Navigator, Debug, and Utility areas. You may also need to again select the black folder Project Navigator icon, located directly under the Run button—the Run button looks like the Play button in iTunes.
Figure 2-4. Your workspace
Figure 2-5. Drag a label onto your canvas
Figure 2-6. Center the label on your canvas. Delete its text and center its content
Note You’ll also see the Vertical Line Constraint just slightly to the right of the center line. That tells you the how close or far apart the distance relationships to the top and bottom of the canvas amongst other things are.
Figure 2-7. Drag a button onto the canvas
Figure 2-8. Choose a color for your background
Figure 2-9. Close the Utilities folder, save your work, and open the Assistant
Figure 2-10. Control-drag from your label to create an outlet
Note The Assistant uses a split-pane editor, but note that you can open the Assistant automatically by Option-clicking a file in the Project Navigator or Symbol Navigator pane.
#import <UIKit/UIKit.h>
@interface helloWorld_01ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *label;
@end
Figure 2-11. Make the IBOutlet a label – this automatically creates your Objective-C code
Figure 2-12. Control-drag from your button to create an action
Figure 2-13. Create the action for your button
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *label;
- (IBAction)button:(id)sender;
@end
Before moving on to step 15, let’s look around and see where we’re at. Back in step 3, I said you have two classes that contain two files: a header file (.h) and an implementation file (.m). Let me talk a little bit about the difference between these two files.
The ViewController class manages the interactions your code has with the display, and it manages the user’s interactions with your code. It contains a view, but it’s not a view itself. You only have a minimal understanding of the ViewController class so far. What I want you to get, though, is that every class consists of two parts: the header (.h) file and the implementation (.m) file.
I want you to read this next part aloud, okay? We tell the computer in a header file what types of commands we will execute in the implementation file. Now, say it again in the context of your code: We tell the computer in the ViewController.h file what types of commands we will execute in the ViewController.m file.
Well, admit it—that wasn’t so bad! Let’s get back to the example.
Figure 2-14. Open your helloWorld_01ViewController’s implementation file
Recall how in step 13 (Figure 2-13) you declared an action when you Control-dragged the button into your header file’s code? Remember that in the header file you declare actions, whereas in the implementation file you implement actions. Here in your ViewController’s implementation file, you’re going to implement the actions that you want to happen when somebody presses the button. Specifically, you want the label to say “Hello World!” How do you do that? Well, you need to type your very first code to start your journey toward geekdom. Yup, you’re going to code text. Take a deep breath and follow along.
Looking at the text of your ViewController’s implementation file, you can see that the clever people at Apple programmed Xcode to write a number of methods already that need to happen in the background just to get your app with label and buttons running on your iPhone. For now, I’ll ignore these methods. Just scroll down to the end until you get to one named - (IBAction)button:(id)sender. Hmm … wait a minute, that’s the code that appeared in step 14, right? Well, almost. That code ended with ; (semicolon) because in the header file, you declared this action. Xcode knows you now need to implement whatever actions you tell it to do inside the squiggly brackets. You need to remember this rule. You’ll use it over and over again.
Note A declaration in the .h file becomes a method in the .m file by replacing the colon with squiggly brackets.
After reaching the implementation of the action you declared in the header, I want you to place your cursor in between the two squiggly brackets, as indicated in the following code. Click there and read below.
Figure 2-15. As you enter the text label.text, autocompletion suggests code. Type the text you want the label to say after the @ directive
Figure 2-16. Save your work with +S
Figure 2-17. Run it with +R
As Figure 2-18 shows, the iPhone Simulator loads your very first app, waits for you to press (click) the button, and then says “Hello World!”
Figure 2-18. The iPhone Simulator is loaded and waiting for the user to press the button. Then it will say “Hello World!”
Congratulations, my friend! You’ve really done something very special today. You may have cursed me a couple of times or floundered here and there, but in getting here you’ve taken that very difficult leap from being a user of technology to being a coder of technology.
We still have a few things to do, so take a break. Walk the dog. Do something that doesn’t involve technology, even if it’s just taking a walk. Take a minute to realize that you’re beginning a long journey, one that will be difficult at times, but one wherein you can hold your head high and say, “Yeah, I code iPhone and iPad apps!”
Running Your iPhone App on the iPad Simulator
As you will see, apart from specifying that the app will be on an iPad rather than an iPhone, the app will be the exact same thing. But let’s go through it so you know that this is indeed the case.
Figure 2-19. Let’s see how your iPhone app runs on the iPhone Simulator
Figure 2-20. The initial screen of the iPad Simulator. If it initially it appears in default screen, click to the right of the highlighted middle dot
Figure 2-21. Click your app’s button and zoom it up with the zoom button
Figure 2-22. Click “Press Me!” It worked on the iPad
Running Your iPad App on the iPad Simulator
At the beginning of this chapter, I made a deal with you. I said you were going to make a very simple app and run it in three ways:
You’ve already completed the first two. But it’s still not really an iPad app. If you look closely, you’ll see pixilation (jagged pixels)—what we often call pseudo iPad style. What you really want to do is program your app specifically for the iPad. A real iPad app is made specifically for the iPad and cannot be run on an iPhone. All the graphics and screen sizes are specifically designed for the iPad and are too large for the iPhone. So let’s go ahead and make an app specifically for the iPad.
Figure 2-23. Open Xcode, select the View-based Application template, and then save a new project file to your desktop
Huh? Yes, that’s what I said. I make my students in the lecture hall redo helloWorld all over again, but now as an iPad app, and I encourage them to try not to peek at their lecture notes. I want them to try doing it on their own. If you need to refer back to earlier steps in this chapter, that’s okay—but then do it again and again until you can do it by memory. Students usually need to do it between three and six times before they can repeat it identically without referencing notes.
Note You may have noticed the Universal option in the drop-down menu in Figure 2-23. Universal allows you to code for the iPhone and the iPad at the same time. When run by your user, the app adapts the screen size to the current device. You’ll use Universal later in the book, but you can experiment with it now.
Now that you’ve created your new apps, you need to do a few organizational chores. Right now, on your beautiful clean open desktop, you only have two folders containing your two helloWorld programs. You need to make a place to store all your programs that will make sense to you as you continue to read this book. Create a subfolder in your Documents folder called My Programs and then save the files helloWorld_01 and helloWorld_02 there by dragging them to that folder. Now, with a fresh, clean, empty desktop, close all programs. Press z+Tab and then z+Q to close everything until only the Finder is left on your screen.
At the ends of chapters, I include this section called “Digging the Code,” wherein I start feeding you insights into the meaning of much of the code that miraculously appeared or that I instructed you to type. What I’ve found, though, is that the human brain makes its own associations if it keeps on doing something over and over again, and certain outcomes occur each time we repeat that action. I’ve found that if I first allow students to fly through huge amounts of code in sheer ignorant bliss, it does a great deal of good because it allows their brains to make connections that only they can make. So here in “Digging the Code” I start feeding you little snippets, just the right ones that connect the dots as to why you put this code here or that code there. Later, as you approach the end of the book, you’ll feel totally comfortable really digging the code and getting into it. For now, take a deep breath, relish the fact that you coded an app in the fundamental ways necessary for iPhone and iPad apps, and I’ll see you in Chapter 3.
Good job!