Xcode offers many templates from which to choose when creating a new application. Sometimes,
though, we want a bit more control over the details of an app than these templates can provide; for this
reason, Xcode also offers an Empty application template. In this blog, we’ll see how to use the Empty
template to build a simple application. So let’s get started!
Start up Xcode and select “Create a new Xcode project.” In the next screen, choose Empty Application,
then click next:
In the following screen, name the project EmptyApp, select the options shown. Note that even though
we are developing here for iPad, the principles involved are the same for iPhone development.
Click Next, choose a location to save the project, and click Create.
The first thing to notice about the Empty application
template is that it provides the AppDelegate class, but
nothing else. We will need to provide our own view
controller and view for the application.
In the menu, select File | New ► then File… In the next
window, choose Objective – C Class, then click Next. In
the window that results, name your new class
MainViewController, make it an instance of
UIViewController, select the checkbox next to “Targeted
for iPad,” and make sure the “With XIB for user interface” check box is unchecked. (We’re going to
create the xib file separately in this case to show how a view is connected to a view controller.) Click
Next. Choose the default location to save the files, and click Create.
Now we’ll create the actual view. Another way to add a new file to the application is to right-click in
the Project Navigator and select New File… from the popup menu. Do so now. In the resulting window,
choose “User Interface” from the list to the left under iOS, then select the Empty document as shown
here:
Click Next, and make sure the Device Family is set to “iPad.” Click Next once again, and in the Save
As text box, enter “MainView.” Click Create to save the file in the default location.
The Project Navigator panel should now appear as shown
here. Note that you may have to drag some files around in the Navigator to properly organize them. Sometimes, Xcode will crash when dragging files – this is a known issue; simply reopen Xcode and continue when this happens.
Select the xib file to open it in Interface Builder. You will see an entirely empty Window. First, select the File’s Owner object. In the Identity Inspector, choose MainViewController for the File’s Owner’s class, as shown below. This will make the MainViewController class the view controller for the view that we will be adding to this xib file.
Next, drag a UIView object from the Library to the
Interface Builder surface. Notice that the UIView
object is properly sized for the iPad device, because
we made the iPad selection in the Device Family
drop down when we created the xib file. Give the
view a nice background color.
Now right-click the File’s Owner object, and drag
from the circle to the right of the “View” item to the view itself. This makes the new UIView object the
MainViewController’s view. At this point, we’re done with Interface Builder (for now).
We need to make this view a subview of the application’s window object, so it can be displayed when
the app runs. Select the AppDelegate.h file, and make the changes shown in bold:
#import "MainViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) MainViewController *mainViewController;
@end
We’ve imported the MainViewController.h file (to obtain access to that object), and also set up a
property of type MainViewController called mainViewController. We will instantiate this object in
the .m file, then add it’s view as a subview of the window object.
Open up the AppDelegate.m file, and once again make the bolded changes. (The listing is on the next
page:)
@implementation AppDelegate
@synthesize window = _window;
@synthesize mainViewController = _mainViewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.mainViewController = [[MainViewController alloc] initWithNibName:nil
bundle:nil];
self.window.backgroundColor = [UIColor whiteColor];
[self.window addSubview:self.mainViewController.view];
[self.window makeKeyAndVisible];
return YES;
}.
..
(We’re not showing the entire file, only the portion that requires changes to be made.)
First, we synthesize the property mainViewController. Following Apple’s recommendation, we
synthesize this property with a name different from that given to it earlier, this makes it impossible to
accidentally use it as an ivar: we must access it using its getter and setter methods.
Next we instantiate mainViewController by first allocating space on the heap for an object of class
MainViewController, then calling initWithNibName: bundle: to initialize the object. In this case, we’ve
already wired up the view controller, view, and File’s Owner in Interface Builder: a value of nil for the
nib name will take the values already provided. A value of nil for the bundle parameter signals the
system to take the current application bundle.
Finally, we add the mainViewController’s view as a subview of the main application window. When
this window is made visible, the subview will be displayed on top of it.
Run the application to verify that the view is shown. At this point, the application is ready for
development to proceed along traditional lines.