Create controller, Controller load view process, Controller view life cycle, multi-controller

Source: Internet
Author: User

In the blog that introduces the four main objects, you can basically understand the process of program initiation:

Main-->uiapplicationmain--> Create instances of UIApplication and app proxy appdelegate and set up proxy---> After the program starts, that is, after the splash screen is displayed, Appdelegate Create UIWindow (can be created automatically, or can be created manually)

The question now is how to create a controller and set it as the root controller for UIWindow, and then load the view from the controller and display it.

directory of this document
    • 1. Three ways to create a controller
    • 2. The view creation process of the controller
    • The life cycle of a 3.Controller view
    • 4. Use of the navigation controller
    • Simple use of 5.UITabBarController
    • 6.tabbarcontroller>>navigationcontroller>>viewcontroller Mainstream framework
-1. Three ways to create a controller1. Three ways to create a controller

1> is created directly from Alloc + init
2> to create a controller by loading the storyboard file
3> to create a controller with the specified xib file

Method One:

1. Create window Self.window is a strong pointer self.window = [[UIWindow alloc]initwithframe:[uiscreen mainscreen].bounds];//2. Create a controller, and set as window's root controller Mkoneviewcontroller *oneviewcontroller = [[Mkoneviewcontroller alloc]init]; Self.window.rootViewController = oneviewcontroller;//3. Set Self.window as the main window and display [Self.window makekeyandvisible];

Method Two:

1. Create Windowself.window = [[UIWindow alloc] Initwithframe:[uiscreen mainscreen].bounds];//2. Load the controller via storyboard, and set Initialviewcontroller as the root controller of window Uistoryboard *SB = [Uistoryboard storyboardwithname:@ "two" bundle:nil]; SB load Mktwoviewcontroller *twoviewcontroller = [sb Instantiateinitialviewcontroller]; Initialize InitialViewControllerself.window.rootViewController with SB = twoviewcontroller;//3. Set the window to be the primary and display [Self.window Makekeyandvisible]; Note If you want to load a different controller in the SB file instead of Initialviewcontroller, create it according to the controller's storyboard ID: [Storyboard instantiateviewcontrollerwithidentifier:@ "Vmid"];

Method Three:

is to let the control in the entire Xib file is managed by the controller, there is no director in the Xib, but the Xib files owner is set to the specified controller, the controller is created with initwithnib//1. Create Windowself.window = [[ UIWindow Alloc]initwithframe:[uiscreen mainscreen].bounds];//2. Use Xib to load the controller and set the root controller to window Mkthreeviewcontroller * Threeviewcontroller = [[Mkthreeviewcontroller alloc]initwithnibname:@ "Mkthreeview" bundle:nil]; Self.window.rootViewController = threeviewcontroller;//3. Set Self.window as the main window and display [Self.window makekeyandvisible];

The use of Xib should be noted:

If you create a controller without explicitly specifying the Xib file (that is, using code like this to create [[Mkthreeviewcontroller Alloc]init]), the default system goes back to find the same name as the controller (but remove the suffix controllers, That is mkthreeview.xib) of the Xib file, if it is found then use the view in this xib as the default view of the controller (provided that the view is already connected, even if not set files owner is also possible).

If it is not found, try to find a xib file (Mkthreeviewcontroller.xib) that is the same as the controller name, and then use the view in this Xib file as the default view of the controller. If you can't find it, create a transparent view (empty view).

It is recommended that such a xib be named as a controller suffix because it acts as a controller, similar to a controller in SB.

-the creation process of the view of the controller2. The view creation process of the controllerBack to the top controller is responsible for the creation of the view that it manages, how is the view created, in generalThe creation of a view is related to how the controller is created, the controller view is created in the following ways:

1. Created by storyboard, after the controller is created, the Loadview method is called automatically to create a view of the controller.
* * At this time the custom controller, because there is no "override" ("Implementation") Loadview method, so the Loadview method inside is based on the view in the storyboard file to create the view.

2. Created by the Xib file, after the controller is created, automatically calls Loadview to create a view of the controller.
* * At this time the custom controller, because there is no "override" ("Implementation") Loadview method, so the Loadview method inside is based on the view in the Xib file to create the view.

3. Rewrite by overriding (implementing) the Uiviewcontroller Loadview method. (You can create a view of the controller by using your own code)
The Loadview method of a controller is to use a custom view.
* * If you want to customize the view for the controller, write it in Loadview and do not write in Viewdidload.
If [Super Loadview] is called in the overridden (implemented) Loadview method, it will still be loaded in the default way.
Rewrite Viewcontroller's Loadview method, use the custom view, and do not call [Super Loadview] when customizing the view.
[Super Loadview]; equivalent to executing a code

If (is the controller created according to storyboard) {   Self.view = view in the controller in storyboard,} else if (xib) {   Self.view = Xib in view;} else {
   self.view = transparent One view}

* * Note: either by loading xib CREATE VIEW, storyboard create view, ultimately relying on Loadview method to create. This method determines the final view.

* * Note: Modify the project file (for example: Xxx.xib, etc., first product-and clean, then the software from the emulator to uninstall, and then run.) )

* * What time does the Loadview method of the controller call?
* * Called when the controller's view is needed (when invoking the Makekeyandvisible method of the UIWindow object, the view needs to be displayed, which means the view is used.) ), this is called "lazy loading".
* * For example, when using Self.view.backgroundColor, to set the background color of the controller's view (you need to use the view at this point), then you will start to create a view of the controller, that is to call the Loadview method. Therefore, if you call Self.view.backgroundColor inside the Loadview method, a dead loop occurs.

* * You can call the controller's Self.isviewloaded method to determine if the current controller's view is already loaded.

* * And when the controller's view is loaded, the Viewdidload method ( called by the system itself ) is called.

The life cycle of the-controller viewThe life cycle of a 3.Controller viewHere are 7 ways to represent the life cycle of a controller view
1. Viewdidload
2. Viewwillappear
3. Viewdidappear
4. Viewwilldisappear
5. Viewdiddisappear
6. Viewwillunload
7. Viewdidunload
Here is a sequence diagram of the calls to these methods: Suppose there are two controllers:
After the program starts: Onecontroller's viewdidload--->onecontroller viewwillappear--->onecontroller viewdidappear

Jump to the second interface: two viewdidload--->one viewwilldisapear--->two viewwillappear---->one viewdiddisappear--- >two's Viewdidappear

Return to the first interface: two viewwilldisapear--->one viewwillappear----> Two viewdiddisappear--->one viewdidappear (one view does not need to be created again)

Jump to the second interface again: As with the first one, two view needs to be created.

-4. Use of navigation Controller4. Use of the navigation controllerBack to the top the navigation controller itself is not much different from other controllers, but the navigation controller can be responsible for the bar of the page, which is used to manage a set of controllers that become its sub-controller (and the concept of a subclass differs from the other)

* * Use steps:
1> Create and initialize a navigation controller: Uinavigationcontroller.
2> set the UIWindow rootviewcontroller to Uinavigationcontroller.
3> adds a child controller to Uinavigationcontroller by calling the push method.
* * Note: Who is the last push in, the current display is which Viewcontroller, in-(BOOL) Application: (UIApplication *) application Didfinishlaunchingwithoptions: (nsdictionary *) launchoptions
Uinavigationcontroller *nav = [[Uinavigationcontroller alloc]init];self.window.rootviewcontroller = nav; The root controller set to window      mkoneviewcontroller *ONEVC = [[Mkoneviewcontroller alloc]init]; [Nav PUSHVIEWCONTROLLER:ONEVC Animated:yes]; Jump page to Onevc view
In a generic application, if you have navigation control, you specify its initial controller when you create the navigation controller, instead of using push to put the initial controller into the stack push:
Oneviewcontroller *ONEVC = [[Oneviewcontroller alloc]init]; Uinavigationcontroller *nav = [[Uinavigationcontroller ALLOC]INITWITHROOTVIEWCONTROLLER:ONEVC]; Self.window.rootViewController = nav;
Navigationcontroller The following methods are also more commonly used:
[Nav Popviewcontrolleranimated:yes]; Eject the stack top controller and return to [Nav Poptorootviewcontrolleranimated:yes]; Eject the top controller in turn until the view of the root controller is displayed
To set some properties of the navigation bar, set them separately in the child controller of the navigation controller:
Set the Navigationitem property of the current controller in the Viewdidload method of each controller.
The specific contents of the Navigationitem property:
* titleProperty
* titleViewProperty
* leftBarButtonItem Properties: Only one button in the upper-left corner can be set
* leftBarButtonItemsProperties: You can set multiple buttons in the upper-left corner
* rightBarButtonItemProperties: Only one button in the upper right corner can be set
* rightBarButtonItemsProperties: You can set multiple buttons in the upper right corner
* backBarButtonItemProperties: Set the button on the next controller, upper left corner. By default, the button text is the same as the title text of the previous controller.
If you use storyboard to set the navigation bar and its word controller, be aware that:when the navigation controller connects to the sub-controller, it chooses relationship Segue:view controllers。

Simple use of -5.uitabbarcontrollersimple use of 5.UITabBarControllerBack to the top is basically the same way Navigationcontroller was created
When adding child controls, you can use the
Viewcontrollers property or Setviewcontrollers: method to add a controller array
However, you cannot use the Childviewcontroller property, which is a read-only property, and here is a simple use example:
1. Create Self.windowself.window = [[UIWindow alloc]initwithframe:[uiscreen mainscreen].bounds];//2. Create a Tabbarcontroller controller and set its child controller to the root controller of window Uitabbarcontroller *tab = [[Uitabbarcontroller alloc]init];o Neviewcontroller *one = [[Oneviewcontroller alloc]init]; Twoviewcontroller *two = [[Twoviewcontroller alloc]init]; Threeviewcontroller *three = [[Threeviewcontroller alloc]init];//    [tab Setviewcontrollers:@[one, both, three]]; Tab.viewcontrollers = @[one, both, Three];self.window.rootviewcontroller = tab;//3. Sets the window as the primary interface and displays the [Self.window Makekeyandvisible];
The navigation bar is set by storyboard, which displays all of the buttons when the navigation bar is displayed, rather than delaying loading as the navigation is set for each sub-controller. Note that when connecting,the choice is relationship Segue -6.tabbarcontroller>>navigationcontroller>>viewcontroller Mainstream framework6.tabbarcontroller>>navigationcontroller>>viewcontroller Mainstream FrameworkBack to the top most of the current programs use the Tabbarcontroller>>navigationcontroller>>viewcontroller mainstream framework, Window's root controller is a Uitabbarcontroller, its sub-controller is multiple uinavigationcontroller, each navigationcontroller can segue or modal out other controllers.

Create controller, Controller load view process, Controller view life cycle, multi-controller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.