Create a Controller
控制器常见的创建方式有以下几种//通过storyboard创建//直接创建ViewController *vc = [[ViewController alloc] init];//指定xib文件来创建ViewController *vc = [ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
Creating a controller from storyboard
//先加载storyboard文件(Test是storyboard的文件名)UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];//接着初始化storyboard中的控制器 初始化“初始控制器”(箭头所指的控制器)ViewController *vc = [storyboard instantiateInitialViewController];//通过一个标识初始化对应的控制器ViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"vc"];
The creation of the Viewcontroller view
Delay Loading of Controller view
The view of the controller is lazily loaded: reload when used
You can use the Isviewloaded method to determine whether a Uiviewcontroller view has been loaded
The controller's view is loaded and the Viewdidload method is called
Multi-Controller
An iOS app rarely consists of a single controller, unless the app is incredibly simple
When there are multiple controllers in the app, we need to manage these controllers
When you have multiple view, you can use a large view to manage 1 or more small view
Controller is the same, with 1 controllers to manage the other multiple controllers
For example, use a controller A to manage 3 controllers B, C, D
Controller A is referred to as the "parent controller" of Controller B, C, D
Controller B, C, d are referred to as controller A's "sub-controller"
//为了便于管理控制器,iOS提供了2个比较特殊的控制器UINavigationControllerUITabBarController
Uinavigationcontroller Sub-controller
UINavigationController的使用步骤初始化UINavigationController设置UIWindow的rootViewController为UINavigationController根据具体情况,通过push方法添加对应个数的子控制器
UinavigationcontrollerSave the sub-controller as a stack@property(nonatomic, copy)Nsarray*viewcontrollers;@property(nonatomic,ReadOnly)Nsarray*childviewcontrollers;//Use the Push method to push a controller into the stack- (void) Pushviewcontroller: (Uiviewcontroller*) Viewcontroller Animated: (BOOL) animated; Use the Pop method to remove the controller//Remove the controller from the top of the stack- (Uiviewcontroller*) Popviewcontrolleranimated: (BOOL) animated;//Return to the specified sub-controller- (Nsarray*) Poptoviewcontroller: (Uiviewcontroller*) Viewcontroller Animated: (BOOL) animated;//Back to root controller (stack bottom controller)- (Nsarray*) Poptorootviewcontrolleranimated: (BOOL) animated;
Modify the contents of the navigation bar
The contents of the navigation bar are determined by the Navigationitem property of the stack top controller
Uinavigationitem has the following properties that affect the contents of the navigation bar//Back button in upper left corner@property(nonatomic, retain)Uibarbuttonitem*backbarbuttonitem;//Middle title View@property(nonatomic, retain)UIView*titleview;//Middle title text@property(nonatomic, copy)NSString*title;//view in upper left corner@property(nonatomic, retain)Uibarbuttonitem*leftbarbuttonitem;Uibarbuttonitem*rightbarbuttonitem//view in upper right corner@property(nonatomic, retain)Uibarbuttonitem*rightbarbuttonitem;
The life cycle of Uiviewcontroller
#Import "ZLOneViewController.h"@implementationZloneviewcontroller/** * View is loaded and will be called */- (void) viewdidload{[SuperViewdidload]; NSLog (@"Window A view is loaded and will call---viewdidload");}/** * View is going to be displayed on the window will be called * /- (void) Viewwillappear: (BOOL) animated{[SuperViewwillappear:animated]; NSLog (@"Window A view is about to be displayed on the window and will call---viewwillappear:");}/** * View will be called (already displayed on the window) */- (void) Viewdidappear: (BOOL) animated{[SuperViewdidappear:animated]; NSLog (@The window will be called (already displayed on the window) when the view is displayed---viewdidappear: ");}/** * View is about to be removed from window (will not be visible) */- (void) Viewwilldisappear: (BOOL) animated{[SuperViewwilldisappear:animated]; NSLog (@"window one view is about to be removed from windows and will be called (soon to be invisible)---viewwilldisappear:");}/** * View is completely removed from the window and will be called (completely invisible) */- (void) Viewdiddisappear: (BOOL) animated{[SuperViewdiddisappear:animated]; NSLog (@"window one view is completely removed from windows and will be called (completely invisible)---viewdiddisappear:");}/** * will be called when the view is about to be destroyed * /- (void) viewwillunload{[SuperViewdidunload]; NSLog (@"Window A view is about to be destroyed when it calls---viewwillunload");}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Development-Uiviewcontroller Controller management