The Simplest View Controller

来源:互联网 发布:数据库性能监控工具 编辑:程序博客网 时间:2024/05/29 21:17

The View Controller

The UIViewController is the base class for all view controllers. When you creating a view contrller, you either subclass this class or its subclasses.

CDAViewController.h

#import <UIKit/UIKit.h>@interface CDAViewController : UIViewController {NSString *left, *right, *up, *down;}- (NSString *)message;@end

CDAViewController.m

#import "CDAViewController.h"#import "CDAView.h"@implementation CDAViewController// The designated initializer.  Override if you create the controller programmatically // and want to perform customization that is not appropriate for viewDidLoad.- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];    if (self) {        // Custom initialization.up = @"up";down = @"down";left = @"left";right = @"right";    }    return self;}- (NSString *)message{switch (self.interfaceOrientation) {case UIInterfaceOrientationPortrait:return up;break;case UIInterfaceOrientationPortraitUpsideDown:return down;break;case UIInterfaceOrientationLandscapeLeft:return left;break;case UIInterfaceOrientationLandscapeRight:return right;break;default:return up;break;}}// Implement loadView to create a view hierarchy programmatically, without using a nib.- (void)loadView {CGRect rect = [UIScreen mainScreen].applicationFrame;CDAView *theView = [[CDAView alloc] initWithFrame:rect];theView.backgroundColor = [UIColor whiteColor];theView.viewController = self;theView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;self.view = theView;[theView autorelease];}- (void)dealloc {    [super dealloc];}@end

We override the initialization method initWithNibName:boundle: in order to initialize the data model.

Because we are creating the view programmatically, we  need to  override the method loadview. 
We configure the view to have flexible height and width. This is achieved by setting theautoresizingMask property with the appropriate value.

The UIViewController's property interfaceOrientation is used to retrieve the current orientation of the device. The property is declared as follows:
@property(nonatomic, readonly) UIInterfaceOrientation interfaceOrientation.

There are four orientations of type UIInterfaceOrientation. The orientations are:

UIInterfaceOrientationPortrait    UIInterfaceOrientationPortraitUpSideDown

UIInterfaceOrientationLandscapeLeft   UIInterfaceOrientationLandscapeRigh

The View

The view has a property use to assign the view controller that is managing the view.

CDAView.h

#import <UIKit/UIKit.h>@class CDAViewController;@interface CDAView : UIView {CDAViewController *viewController;}@property(nonatomic, retain) CDAViewController *viewController;@end

CDAView.m
#import "CDAView.h"#import "CDAViewController.h"@implementation CDAView@synthesize viewController;- (void)drawRect:(CGRect)rect {    [[viewController message] drawAtPoint:CGPointMake(80.0,30.0) withFont:[UIFont systemFontOfSize:50]];}- (void)dealloc {    [super dealloc];}@end

The application delegate

#import <UIKit/UIKit.h>@class CDAViewController;@interface ViewDemoAppDelegate : NSObject <UIApplicationDelegate> {    UIWindow *window;CDAViewController *viewController;}@property (nonatomic, retain) IBOutlet UIWindow *window;@property (nonatomic, retain) CDAViewController *viewController;@end

#import "ViewDemoAppDelegate.h"#import "CDAViewController.h"@implementation ViewDemoAppDelegate@synthesize window;@synthesize viewController;#pragma mark -#pragma mark Application lifecycle- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        // Override point for customization after application launch.    self.viewController = [[CDAViewController alloc] initWithNibName:nil bundle:nil];[self.window addSubview:viewController.view];    [self.window makeKeyAndVisible];    return YES;}- (void)dealloc {    [window release];[viewController release];    [super dealloc];}@end