Viewcontroller is an important part of IOS applications and an important bridge between application data and views. viewcontroller manages multiple views in applications. The ios sdk provides many native viewcontrollers to support standard user interfaces, such as the table View Controller (uitableviewcontroller), navigation controller (uinavigationcontroller), and label bar controller (uitabbarcontroller) and iPad proprietary uisplitviewcontroller.
By structure, you can divide all viewcontrollers of IOS into two types:
1. viewcontroller is mainly used to display content. This viewcontroller is mainly used to display content for users and interact with users, such as uitableviewcontroller and uiviewcontroller.
2. used to control and display viewcontroller of other viewcontrollers. This viewcontroller is generally a container of viewcontroller. For example, uinavigationcontroller and uitabbarcontroller. They all have a property: viewcontrollers. Uinavigationcontroller indicates a stack structure that pushes a viewcontroller or pop once. Therefore, the latter viewcontroller generally depends on the former viewcontroller. Uitabbarcontroller represents an array structure, and each viewcontroller is tied.
Viewcontroller usage
UIViewController can be created in two ways: 1. xib, 2. Code
1) xib Mode
Command + N create a file, select Cocoa Touch UIViewController subclass, SubClass of UIViewController, and check with XIB for user interface. Define the ViewController name as MainViewController, and generate three files: MainViewController. h MainViewController. m MainViewController. xib. In the AppDelegate. m file
Load the xib file in the didfinishlaunchingwitexceptions method.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; MainUIViewController *vc=[[MainUIViewController alloc]initWithNibName:@"MainUIViewController" bundle:nil]; self.window.rootViewController=vc; [self.window makeKeyAndVisible]; return YES;
After the xib file is loaded, drag several controls in the xib file to see the effect.
2) code creation
Command + N create the UIViewController file, which is used in the didfinishlaunchingwitexceptions method of the AppDelegate. m file. Use the code to create a view in the loadView method of UIViewController.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; // MainUIViewController *vc=[[MainUIViewController alloc]initWithNibName:@"MainUIViewController" bundle:nil];// self.window.rootViewController=vc; RootViewController *rc=[[RootViewController alloc] init]; self.window.rootViewController=rc; [self.window makeKeyAndVisible]; return YES;}
Create View
- (void)loadView{ [super loadView]; UIView *view=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame]; view.alpha=0.5; view.backgroundColor=[UIColor cyanColor]; self.view=view;}
Viewcontroller Lifecycle
The life cycle of the iOS application is previously written. The Life Cycle of ViewController is written here, which is more like the life cycle of the Android Activity (see the figure at the end of the article ). The life cycle of ViewController is initialized, loaded, destroyed, and ended.
1) Init Method
Initialize viewcontroller itself.
2) loadview Method
When a view needs to be displayed but it is nil, viewcontroller calls this method.
If the Code maintains the view, you need to override this method. If you use XIB to maintain the view, you do not need to override it.
3) viewdidload Method
After loadview is executed, viewdidload is executed. No view is available at loadview, and view is created at viewdidload.
4) viewdidunload Method
This method is called when the system memory is tight. When the memory is tight, didreceivememorywarning is the only way to release useless memory before iPhone OS 3.0, however, the viewdidunload Method for OS 3.0 and later is a better method.
In this method, set all iboutlet (whether it is property or instance variable) to nil (the system release view has already been release ).
In this method, other view-related objects, other objects created at runtime (but not required by the system), objects created in viewdidload, and cached data are released.
Viewdidunload is generally considered as the image of viewdidload, because when the view is requested again, viewdidload will be re-executed.
5) dealloc
Release other resources or memory.
Lifecycle chart of viewController
View loading process of ViewController, see (loadView)
1) loadview
For details about how to uninstall the View, see (unLoadView)
2) unloadview
/**
* @ Author Zhang xingye * http://blog.csdn.net/xyz_lmn* iOS entry group: 83702688
* Android Development Group: 241395671
* My Sina Weibo:@ Zhang xingye TBOW*/
Refer:
Https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007457-CH1-SW1
Http://xcodev.com/341.html
Http://iostrack.com/post/2012-07-20/40029700941
Activity Lifecycle