What is the process of creating a UIViewController (which method follows)?

There are many ways to override, for example, initWithNibname: awakeFromNib , loadView , viewDidLoad , viewDidAppear: layoutSubviews , and I just can't decide in which order this method is called.

I just redefine one of them by heart.

Any detailed explanation?

+54
iphone uiviewcontroller
Feb 24 '11 at 16:38
source share
7 answers

The Cocoa scenario happens with a bunch behind the scenes .

1. object viewController

At its most basic, viewController is a common controller object. When it is first allocated initialized, it does not have a view object associated with it. A view is created only when (and if) is required. Thus, without considering the view, the life cycle of the viewController is the same as any other object:

 UIViewController * myVC = [[UIViewController alloc] initWith...]; ... [myVC release]; 

The designated initializer for viewControllers is -initWithNibname:bundle: If you specify a tip, viewController can automatically load its view from this nib and attach any IBOutlets that you have defined (see below for more details).

2. View loading and unloading

The viewController function will load its view as needed. This usually happens when the -view method -view called for the first time and can happen at any time in your program, depending on how you initialize your user interface. The view can also be destroyed and restarted several times during the entire duration of your program, agan, depending on how you manage your user interface. When the viewController determines that its view is required but not yet loaded, the -loadView method will be called. The main message flow looks something like this:

 view loadView viewDidLoad 

Note that if you override the -view method, -loadView and viewDidLoad will not be called automatically. If you override -loadView , you must set the viewController view property. Otherwise, the next call to -view will call the boot process again.

You can also unload a view at any time during the life of your program by simply setting the view property to nil . The default implementation of -didReceiveMemoryWarning will automatically default if there is no supervisor in the view (i.e. if it is not currently part of the active view hierarchy). The message flow is as follows:

 view = nil viewDidUnload 

2a. Software Download Software

If you decide to override -loadView , you can create a view, subviews, other viewControllers, and any connections between these objects in any way. Of course, this means that you are also responsible for managing memory in relation to the objects being created. If your subclass overrides -loadView , it must be initialized using nil for nibName and bundle .

2b. Download image from the bottom

If you use a nib file, the default implementation of -loadView will automatically open that nib file, instantiate its objects, add any connections between them, and take care of your memory management.

Things get a little more complicated with nib files, as this happens behind the scenes. The -awakeFromNib method -awakeFromNib called for each object that is created when the nib file is loaded, and there is no guarantee that other objects in the nib file will be fully loaded when it is called.

3. Display views

-viewWillAppear: -viewDidAppear: -viewWillDisappear: and -viewDidDisappear: only called when the view is displayed or hidden on the screen, especially during animated transitions from one view to another. These methods can be called many times during the life of your program, as the views change places and go out in your navigation scheme.

4. View layout

The -layoutSubviews method -layoutSubviews not part of the UIViewController . It is called for UIView objects when their boundaries have been changed. If you use a custom subclass of UIView in your program, you can use this method to create a custom subview layout instead of relying on Cocoa's default autosave methods.

5. Putting it all together

Due to complexity, there are many different ways for this process, but a normal timeline might look something like this:

 -[viewController initWithNibname:Bundle:] -[viewController awakeFromNib] -[viewController loadView] -[view awakeFromNib] -[viewController viewDidLoad] -[viewController viewWillAppear] -[viewController viewDidAppear] ... -[viewController viewWillDisappear] // user navigated away -[viewController viewDidDisappear] ... -[viewController viewWillAppear] // user navigated back -[viewController viewDidAppear] ... -[viewController viewWillDisappear] // user navigated away -[viewController viewDidDisappear] ... -[viewController setView:nil] // memory warning, perhaps -[viewController viewDidUnload] ... -[viewController loadView] // user navigated back -[view awakeFromNib] -[viewController viewDidLoad] -[viewController viewWillAppear] -[viewController viewDidAppear] ... 
+172
Feb 24 '11 at 19:09
source share

Recently, I looked again and created a test project: https://github.com/Janek2004/ViewControllerTest

Run the project on the iOS simulator to see the execution order of the UIViewController subclass. The order may differ if we use a Nib file instead of a storyboard or view controller.

  • - [ViewController initWithCoder:] Removing data from nib or storyboard
  • - [ViewController awakeFromNib] Prepares the receiver for the service after downloading it from the Builder interface archive or nib file.
  • - [ViewController loadView] You should never call this method directly. The view controller calls this method when its view property is requested, but it is currently zero. This method loads or creates a view and assigns it to a view property.
  • - [ViewController viewDidLoad] This method is called after the view controller has loaded its view hierarchy into memory.
  • - [ViewController viewWillAppear:] This method is called before the recipient view is added to the view hierarchy and before any animations are configured to display the view.
  • - [ViewController viewWillLayoutSubviews] Called to notify the view controller that its view is intended to compose its routines. When changes in views change, the view adjusts the position of its subzones. The view controller can override this method to make changes before the view sets out its approaches.
  • - [ViewController viewDidLayoutSubviews] Called to notify the view controller that its view has just laid out its subitems. When the boundaries are changed to represent view controllers, the view adjusts the position of its subzones, and then the system calls this method. However, this method that is called does not indicate that the individual layouts of the views of the views have been adjusted. each subview is responsible for setting its own layout.
  • - [ViewController viewDidAppear:] Notifies the view controller that its view has been added to the view hierarchy. You can override this method to perform additional tasks related to the presentation of the view.

  • - [ViewController viewWillDisappear:] Notifies the view controller that its view will be removed from the view hierarchy. This method is called in response to a view that is removed from the view hierarchy. This method is called before the view is actually deleted and before any animations are configured. Notifies a view so that its view is added to the view hierarchy. You can override this method to perform additional tasks related to presenting the presentation.

  • - [ViewController viewDidDisappear:] Notifies the view controller that its view has been removed from the view hierarchy.
+32
Feb 05 '14 at 17:20
source share

Another key point in this process is when layoutSubviews is called in any subheadings. It was at this moment, and not earlier, that any restrictions configured in the storyboard were applied. If you need to make any adjustments to the view sub-points based on limited coordinates, you should do this in layoutSubviews. If you do this in viewDidLayoutSubviews, it will be too early, since these subheadings have not yet applied their limitations (because the documentation says: β€œEach subordination is responsible for setting its own layout.”) And if you do this in viewDidAppear, it will be too late, as the user will see that your subzones change coordinates. So, another important step in this process:

 -viewController viewWillAppear -viewController viewWillLayoutSubviews -viewController viewDidLayoutSubviews ---> viewController.[any subview] layoutSubviews -viewController viewDidAppear 
+7
Nov 22 '14 at 3:19
source share

from the Apple UIViewController documentation:

When you define a new subclass of UIViewController, you must specify the views that the controller should control. There are two mutually exclusive ways to specify these views: manually or using the nib file. If you specify views manually, you must implement the loadView method and use it to assign the root view for the view property to the object. If you specify the views using the nib file, you should not override the loadView, but instead create the nib file in Interface Builder and then initialize the View controller object using the initWithNibName: bundle: method. Creating views using the nib file is often easier because you can use Interface Builder to create and customize your views graphically (as opposed to programmatically). However, both methods have the same ultimate goal, which is to create an appropriate set of representations and expose them through a view property.

From the head:

  • initWithNibname
  • loadView (manual loading)
  • viewDidiLoad
  • viewDidAppear

no clue where layoutSubviews comes in

+4
Feb 24 '11 at 17:09
source share

I usually resolve this issue by placing the NSLog (or breakpoints) in all of these delegates, including the application launch delegate, and following the order in the debugger.

+1
Feb 24 '11 at 16:46
source share
 - This is related to view only:
 -viewWillAppear:
 -viewDidAppear: 
 -viewWillDisappear: and 
 -viewDidDisappear: 

 are only called when the view is being displayed.

 -viewController viewDidLoad
 -viewController viewWillAppear
 -viewController viewDidAppear

 other methods

 -viewController viewDidDisappear
 -viewController viewWillDisappear 
 -viewController viewDidUnload
+1
Nov 21 '13 at 8:56
source share

I want to thank e.James for his excellent description. I cannot comment on the post yet, but for a quick visual illustration, see this flowchart in the View Controller Programming Guide. And I understand that this is off topic, but for a graph of the sequence of launching the application, see the iOS Application Programming Guide.

0
May 03 '13 at 22:00
source share



All Articles