Uiviewcontroller View Controller, inherited from Uiresponder, function: Manage views and respond to events
Function:
1. Share the work of appdelegate
2. Independence of the module, can improve the reusability
To create a Uiviewcontroller object:
UIViewController *viewController=[[UIViewController alloc]init];
Uiviewcontroller itself with a uiview, the default size is the same as the screen size.
Each window has a root view, and if you do not assign a value to the root view, the default root view is itself (window)
self.window.rootViewController=viewController;//设置self.window的根视图
Custom View Controller
@implementation myviewcontroller //Specifies the initialization method (the method that must go when the object is initialized)//Do not normally do anything in the Uiviewcontroller specified initialization method-(ID) Initwithnibname: (NSString*) Nibnameornil Bundle: (NSBundle*) nibbundleornil{ Self=[SuperInitwithnibname:nibnameornil Bundle:nibbundleornil];if( Self) {NSLog(@'%s ', __function__); }return Self;}//Load View-(void) Loadview {//If you write this method, be sure to initialize the view that comes with Uiviewcontroller Self. View=[[UIViewAlloc]initwithframe:[[uiscreen Mainscreen]bounds]];//override method of parent class[SuperLoadview];//If the above do not do, Loadview and Viewdidload will walk two times, this time there are inexplicable errors NSLog(@'%s ', __function__);}//View loading complete-(void) viewdidload{//write this must be to the Uiviewcontroller own view initialization[SuperViewdidload];//Set background color of view Controller view Self. View. BackgroundColor=[UicolorGraycolor];UILabel*label=[[UILabelAlloc]initwithframe:cgrectmake ( -, -, $, +)]; Label. BackgroundColor=[UicolorYellowcolor]; Label. Text[Email protected]"This is the label."; [ Self. ViewAddsubview:label]; [Label release]; Uitextfield *textfield=[[uitextfield Alloc]initwithframe:cgrectmake ( -, -, $, +)]; TextField. BackgroundColor=[UicolorRedcolor]; TextField. Placeholder[Email protected]"Text input box";//Set agent for TextFieldTextField. Delegate= Self; [ Self. ViewAddsubview:textfield]; [TextField release];UIButton*button=[UIButtonButtonwithtype:uibuttontypesystem]; button. Frame=cgrectmake ( -, -, $, +); button. BackgroundColor=[UicolorCyancolor]; [Button settitle:@"button"Forstate:uicontrolstatenormal];//Add a Click event to the button[Button AddTarget: SelfAction@selector(buttonaction:) forcontrolevents:uicontroleventtouchupinside]; [ Self. ViewAddsubview:button]; [Button release];NSLog(@'%s ', __function__);}//Receive memory warning- (void) Didreceivememorywarning {[SuperDidreceivememorywarning];NSLog(@"Clean up the memory.");//Dispose of any resources, can be recreated.}#pragma mark--button Click Event---(void) Buttonaction: (UIButton*) sender{ Self. View. BackgroundColor=[UicolorColorwithred:colorvalue Green:colorvalue Blue:colorvalue Alpha:1.0];}#pragma mark--Implementing the Proxy Method- --(BOOL) Textfieldshouldreturn: (Uitextfield *) textfield{[TextField Resignfirstresponder];return YES;}@end
Screen rotation
- (void) Viewdidload {[SuperViewdidload];additional setup after loading the view. Self. View. BackgroundColor=[UicolorGreencolor];UIView*topview=[[UIViewAlloc]initwithframe:cgrectmake (0, -, $, -)]; TopView. BackgroundColor=[UicolorCyancolor]; [ Self. ViewAddsubview:topview]; TopView. Tag= -; [TopView release];}//method of rotation support//Whether to allow rotation-(BOOL) shouldautorotate{return YES;}//allowable direction of rotation-(Nsuinteger) supportedinterfaceorientations{returnUiinterfaceorientationmaskall;}//How to go when the screen rotates. iOS8.0 will support-(void) Viewwilltransitiontosize: (cgsize) Size Withtransitioncoordinator: (ID<UIViewControllerTransitionCoordinator>) coordinator{//Get the width and height of the screenNsstringfromcgsize (size);if(Size. Width>size. Height) {//Horizontal screen UIView*view=[ Self. ViewViewwithtag: -]; View. Frame=cgrectmake (0, -, size. Width*2/3, -); }Else{UIView*view=[ Self. ViewViewwithtag: -]; View. Frame=cgrectmake (0, -, size. Width*2/3, -); }NSLog(@"The screen is rotated.");}
Uiviewcontroller is the kernel of MVC design pattern?
MVC is a framework-level design pattern.
M is model, mainly in the building data model (i.e. the structure of the data)
V is the view, all the controls we can see are view,view the main function is to show the data.
C is the controller, mainly control the communication between M and V.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UI Basics: View Controller. Screen rotation. MVC