programming, chances are you will not find that the View-based Application template helps
clarify a UIView, a UIViewController, and their relationship. To help make their relationship
clearer, you should understand what the View-based Application template accomplishes automatically.
Unlike a View-based Application template, a Window-based Application template requires
understanding UIViews and UIViewControllers. When using the Window-based Application
template, you must manually create a view and a view controller and wire them together. In
this project, you create a single view application starting with a Window-based Application
template. Creating a Window-based Application should solidify your understanding of the
steps used by Xcode when creating a View-based application.
1. Create a new Window-based Application and name it SimpleWindow.
2. CTRL-click the Resources folder and select New File. Select User Interface under iOS and
select View to create a new xib. Name the xib FirstViewController.xib.
3. Select File | New | New File. Add a UIViewController named FirstViewController. Xcode
should create FirstViewController.h and FirstViewController.m. Be certain the check box
to create a xib is not checked.
4. Open SimpleWindowAppDelegate.h and either import the FirstViewController
or use an @class forward declaration. Add a UIViewController property to
SimpleWindowAppDelegate.h so that it appears the same as Listing 7-5.
Listing 7-5 SimpleWindowAppDelegate.h
#import <UIKit/UIKit.h>
@class FirstViewController;
@interface SimpleWindowAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
FirstViewController *rootViewController;
}
@property (nonatomic, retain) IBOutlet FirstViewController
*rootViewController;
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end
5. Modify SimpleWindowAppDelegate.m so that it appears like Listing 7-6. Notice you
must synthesize rootViewController and add its view to the window as a subview in the
delegate’s applicationDidFinishLaunching: method.
Listing 7-6 SimpleWindowAppDelegate.m
#import "SimpleWindowAppDelegate.h"
#import "FirstViewController.h"
@implementation SimpleWindowAppDelegate
@synthesize window;
@synthesize rootViewController;
-(void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:rootViewController.view];
[window makeKeyAndVisible];
}
-(void)dealloc {
[window release];
[rootViewController release];
[super dealloc];
}
@end
6. Select FirstViewController.xib to display it in Interface Builder. Select the File’s Owner
and then select View | Utilities | Identity from the main menu. Notice that the class of the
File’s Owner isn’t set.
7. Change its class to FirstViewController from the pull-down in the Object Identity Inspector
pane.
8. Select the view, select Object Attributes in the Inspector pane, and change the view’s color.
9. Select the File’s Owner and click the Connections button in the Inspector pane, and then
connect the view outlet to the view you added to the document window.
10. Save FirstViewController.xib and select MainWindow.xib to open it in Interface Builder.
11. Notice that there is no UIViewController or view set in the document window.
12. Scroll down in the list of objects and drag a view controller from the library to the editing
pane. With the View Controller selected, go to the Object Identity Inspector pane and set its
class to FirstViewController (Figure 7-1).
13. In the Object Attributes Inspector pane, change its NIB Name to FirstViewController.
14. Select Simple Window App Delegate (one of the icons to the left of the editing pane).
Select the Connections Inspector pane; notice the rootViewController outlet. Connect this
to the view controller just added (Figure 7-2).
Select the Connections Inspector pane; notice the rootViewController outlet. Connect this
to the view controller just added (Figure 7-2).
15. Save your changes.
16. Click Run to build and run your application. The view in FirstViewController.xib will be
loaded into the window and displayed.
loaded into the window and displayed.
In Step 14, you connected the FirstViewController to the application’s delegate.
This was an important step; it allowed the nib to set the delegate’s root view controller
for you. The root view controller is the UIViewController that is first loaded by an
application delegate. Remember, the application knew to load MainWindow.xib because
it was in the application’s Info.plist. The application loaded MainWindow.xib, saw the
FirstViewController object that was added to the document window, and saw that the
This was an important step; it allowed the nib to set the delegate’s root view controller
for you. The root view controller is the UIViewController that is first loaded by an
application delegate. Remember, the application knew to load MainWindow.xib because
it was in the application’s Info.plist. The application loaded MainWindow.xib, saw the
FirstViewController object that was added to the document window, and saw that the
the controller came from FirstViewController.xib. Because of the object, variable, and nib
settings, the application knew to allocate and initialize a FirstViewController instance from
FirstViewController.xib when loading MainWindow.xib. Because these relationships were
established in Interface Builder, no manual code was necessary. This is how the View-based
Application template builds a simple application, which you just duplicated manually using
the Window-based application template.
NOTE
In this example, you manually created a xib and linked it to its associated view
controller. Step 3 specifically instructed you not to check the check box that also
created a xib; had you checked the check box, Xcode would have created a xib
and automatically made most of these connections for you.
No comments:
Post a Comment