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
- The Simplest View Controller
- The simplest Singleton
- The Model-View-Controller Architecture
- 【View Controller】Configuring the Initial View Controller at Launch
- The simplest Spring application to the beginner
- Failed to instantiate the default view controller
- Failed to instantiate the default view controller
- Failed to instantiate the default view controller
- 【收藏】 The Simplest Anti-IF Code
- The Simplest App on MAC OSX
- view controller
- View Controller
- The Model-View-Controller Pattern —— MVC 模式
- Improve the Recipe App With a Better Detail View Controller
- Failed to instantiate the default view controller for UIMainStoryboardFile
- Failed to instantiate the default view controller for UIMainStoryboardFile
- Failed to instantiate the default view controller for UIMainStoryboardFile
- Failed to instantiate the default view controller for UIMainStoryboardFile 'MainStoryboard'
- 计算几何 线段的各种情况 [好题]
- 嵌入式Linux学习5——Shell编程
- NeHe OpenGL教程 第三课 添加颜色
- 嵌入式根文件系统制作
- 【总结】2-SAT
- The Simplest View Controller
- 语言表达能力必须的, 文学必须的
- 不用去电影院 3D电影格式/播放全攻略
- http://download.csdn.net/download/dev_eric_cf/2818797
- 我附加数据库文件,sqlserver2008 sp1说版本地,打补丁sp2,说支持662版本及更低,高了低了都不行, 服了微软的东西
- varchar和Nvarchar区别
- Java IO(二)
- 求职PK考研
- ANSI字符集和Unicode字符集