Wednesday, May 29, 2013

Using a View-Based Application Template

1. Create a new View-based Application using the template. Name the project SimpleView.

2. Expand the Classes group and notice XCode created the SimpleViewViewController
class for you. Expand Resources and notice the template generated a separate nib,
SimpleViewViewController.xib, for the SimpleViewViewController.

3. Open SimpleViewViewController.h and add a label and method for changing the label’s
value. Make the label an IBOutlet and the method an IBAction (Listing 7-1).

Listing 7-1   SimpleViewViewController.h

#import <UIKit/UIKit.h>
@interface SimpleViewViewController : UIViewController {
UILabel * theLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *theLabel;
-(IBAction) changeLabelValue: (id) sender;
@end


4. Open SimpleViewViewController.m and add the IBOutlet and IBAction definitions
(Listing 7-2).

Listing 7-2   SimpleViewViewController.m

#import "SimpleViewViewController.h"
@implementation SimpleViewViewController
@synthesize theLabel;
-(IBAction) changeLabelValue : (id) sender {
  [theLabel setText:@"Hello World."];
  UIButton *theBut = sender;
  NSLog(theBut.currentTitle);
  theBut.enabled = NO;
  [theBut setTitle:@"Pressed Already" forState:
UIControlStateDisabled];
}
-(void)dealloc {
  [theLabel release];
  [super dealloc];
}
@end


5. Select SimpleViewViewController.xib to display it in Interface Builder and change the
view’s color. Add a UILabel and a UIButton to the UIView.

6. Notice that SimpleViewViewController is the File’s Owner. Connect SimpleViewView Controller’s theLabel outlet to the label.

7. Connect SimpleViewViewController’s changeTheLabel action to the button. Select Touch  Up Inside.

8. Save your changes.

9. Click Build And Go to run the application.

Take a moment to examine what the View-based Application template did for you.  It created the SimpleViewViewController.xib and it also created a UIViewController  subclass, SimpleViewViewController, by creating the SimpleViewViewController.h and  SimpleViewViewController.m files. Moreover, it added the controller to the delegate
(Listing 7-3).

Listing 7-3   SimpleViewAppDelegate.h
 
#import <UIKit/UIKit.h>
@class SimpleViewViewController;
@interface SimpleViewAppDelegate : NSObject <UIApplicationDelegate> {
  UIWindow *window;
  SimpleViewViewController *viewController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain)    IBOutlet SimpleViewViewController
*viewController;
@end
 

In the delegate, the template created the application’s window and view controller as
outlets (Listings 7-3 and 7-4). In the delegate’s applicationDidFinishLaunchingWithOptions:
method, the template added the view controller’s view to the window and then displayed the
window. Notice that nowhere does the code allocate or initialize its window or view controller.
Instead, Info.plist specifies that MainWindow.xib is the application’s main nib, so it knows to
load MainWindow.xib and the nib handles window and view controller initialization.

Listing 7-4   SimpleViewAppDelegate.m

#import "SimpleViewAppDelegate.h"
#import "SimpleViewViewController.h"
@implementation SimpleViewAppDelegate
@synthesize window;


@synthesize viewController;
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [window addSubview:viewController.view];
  [window makeKeyAndVisible];
  return YES;
}
-(void)dealloc {
  [viewController release];
  [window release]; [super dealloc];
}
@end


In the MainWindow nib, the template set the nib’s file’s owner to UIApplication. The
template set SimpleViewAppDelegate as the application’s delegate and set the delegate’s
window to the window in MainWindow.xib.

The template also added a view controller to MainWindow.xib and set it as the delegate’s
root view controller. Every delegate must have a root view controller. The root view controller in
MainWindow.xib comes from the SimpleViewViewController.xib, also created by the template.

The template created the UIView in its own xib, SimpleViewViewController.xib. It set
SimpleViewViewController.xib’s file’s owner to SimpleViewViewController. It also set the
controller’s view to the view in the xib.

No comments:

Post a Comment