Your First iOS App: 100% Programmatically (Xcode 7.2, Objective-C)

Part 1

This is an updated version of a tutorial created by Austin Louden. This tutorial follows Apple’s old “Your First iOS App” tutorial, but implements all of the user interface elements programmatically in Xcode 7.2.

Austin was kind enough to create a two-part tutorial but part 1 is quite outdated. This article is simply an updated version of part 1 and still leads into Austin’s part 2 which can be found here. As such, much of this article is paraphrased from his part 1.

To learn more about my work, visit my website.

This tutorial is intended for developers transitioning to iOS that don’t want to deal with Storyboarding or Interface Builder. It implements the “Hello World” app from Apple’s documentation entirely in code.

At the end of this tutorial, you will have an app that looks like the one on the left. Upon clicking on the text field at the top, the system-provided keyboard appears and the user may type in their name. The user may then dismiss the keyboard by clicking “Done.” Finally, when the user clicks “Hello,” the label displays “Hello,” followed by the user’s name.

The first step is to open Xcode and click on “Create a new Xcode project” In this tutorial, I am using version 7.2.

We’re going to use a “Single View Application.”

Fill out the relevant fields. Depending on your preference, you may select a specific device. We will be using Objective-C here. Core Data is not required for this tutorial.

Prior to saving your project to your computer, ensure that “Create Git repository on” is checked and “My Mac” is selected.

By default, “Single View Application” is not programmatically oriented. Xcode creates some default files that we don’t want. Let’s go ahead and delete “Main.storyboard” by selecting it and clicking “delete” on the keyboard. When prompted, select “Move to Trash” instead of “Remove Reference.” Additionally, let’s go ahead and delete “ViewController.h” and “ViewController.m.”

In order to truly rid ourselves of the storyboard world, we need to modify the “info.plist.” Expand the sub-folder titled “Supporting Files” by clicking on the adjacent triangle. Select the file within the “Supporting Files” folder titled “Info.plist.” Select the row with the text “Main storyboard file base name” and delete it by hitting your “delete” key.

Now that we are free of the oppressive shackles of the storyboard, navigate to the AppDelegate.m file by clicking the file browser on the left side of the screen.

The App Delegate file is your application’s way of communicating with the operating system, and includes several useful methods.

We’ll mostly be dealing with “application didFinishLaunchingWithOptions”, which is called immediately after the application launches.

If you click on the Run button in the top left, your application should build with a blank white screen.

This is all good and well but our application doesn’t show us anything.

In order to begin building an app programmatically, we’ll need to introduce a ViewController class. Click ⌘N to create a new file.

In the “Source” under “iOS,” select Cocoa Touch Class and click “Next.”

You can choose the name of your class, but make sure to set it as a subclass of UIViewController below. I named mine “MainViewController”. The UIViewController is a fundamental class of iOS development, and is used to make and handle interaction with elements in the user interface. Ensure “Objective-C” is selected under “Language.” Click Next, and then Create.

You’ll notice that two files have appeared in the Navigator on the left, “MainViewController.h” and “MainViewController.m.” Each file has some boilerplate code.

We’ll come back to those in a bit — for now, go back to your AppDelegate.m file.

First, make sure to import your MainViewController class at the top of the AppDelegate.m file:

Next, let’s take a look at the “application didFinishLaunching” method — we’ll want to add some code matching the one below

Line 3 instantiates an object of the UIWindow class set to the bounds of the iPhone’s screen.

Line 6 sets the background color of the window to white.

Line 8 instantiates an object of our MainViewController class called “viewController.”

Line 10 sets “viewController” as the window’s root view controller.

Finally, line 12 makes the window visible.

Running the app again should give you a white screen and now you have a view to modify programmatically. In the next part of this tutorial, we’ll handle all of the on-screen elements.

Part 2 of this tutorial is provided by @austinlouden. I hope you found this tutorial helpful in understanding programmatic iOS development.

Continue to Part 2