Managing the View
-
view
property – isViewLoaded
– loadView
– viewDidLoad
-
title
property – viewDidUnload
Deprecated in iOS 6.0– viewWillUnload
Deprecated in iOS 6.0
view
The view that the controller manages.
Discussion
The view stored in this property represents the root view for the view controller’s view hierarchy. The default value of this property is nil
.
If you access this property and its value is currently nil
, the view controller automatically calls the loadView
method and returns the resulting view.
Each view controller object is the sole owner of its view. You must not associate the same view object with multiple view controller objects. The only exception to this rule is that a container view controller implementation may add this view as a subview in its own view hierarchy. Before adding the subview, the container must first call its addChildViewController:
method to create a parent-child relationship between the two view controller objects.
Because accessing this property can cause the view to be loaded automatically, you can use the isViewLoaded
method to determine if the view is currently in memory. Unlike this property, the isViewLoaded
property does not force the loading of the view if it is not currently in memory.
The UIViewController
class can automatically set this property to nil
during low-memory conditions and also when the view controller itself is finally released.
For more information about how a view controller loads and unloads its view, see “Resource Management in View Controllers”.
详细描述:
存在这个属性中的视图代表视图控制器类层次的根视图。默认的值是nil.
如果你访问这个属性,并且它的值为nil,那么视图控制器会自动调用loadView方法,返回得到的视图。
每个视图控制器对象是它的视图的唯一所有者。你不能把同一个视图对象关联到不同的视图控制器对象中。唯一的例外是,在实现一个容器控制器时,它会把这个视图作为视图控制器自己类层次的子视图。在添加子视图中去,视图容器必须先调用子视图控制器的addChildViewController:方法在两个控制器之间创建父子关系。
由于获取这个属性会导致视图被自动加载,你可以使用isViewLoaded方法判断视图是否正在内存中。不象view属性,isViewLoaded不强迫加载这个视图。
视图控制器在内存较低的情况下会自动地把这个属性设置为nil,并且这时候视图控制器自己也被释放了。
对于视图控制器加载卸载视图的详细情况,可以参考“Resource Management in View Controllers”.
loadView
Creates the view that the controller manages.
Discussion
You should never call this method directly. The view controller calls this method when its view
property is requested but is currently nil
. This method loads or creates a view and assigns it to the view
property.
If the view controller has an associated nib file, this method loads the view from the nib file. A view controller has an associated nib file if the nibName
property returns a non-nil
value, which occurs if the view controller was instantiated from a storyboard, if you explicitly assigned it a nib file using the initWithNibName:bundle:
method, or if iOS finds a nib file in the app bundle with a name based on the view controller’s class name. If the view controller does not have an associated nib file, this method creates a plain UIView
object instead.
If you use Interface Builder to create your views and initialize the view controller, you must not override this method.
You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view
property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super
.
If you want to perform any additional initialization of your views, do so in the viewDidLoad
method.
你不要直接调用这个方法。当view 属性被访问并且当前为nil,视图控制器会调用这个方法。这个方法加载或者创建视图,并且把这个视图赋给view属性。
如果视图控制器有想关联的nib文件,那么这个方法会从nib文件加载这个类。如果nib属性返回的是non-nil值,那么视图控制器有相关联的nib文件。如果视图控制器没有相关联的文件,那么,这个方法创建一个空白的视图对象。
如果你用Interface Builder创建视图以及初始化视图控制器,那么你不要override这个方法。
你可以override这个方法以便手动创建视图。如果你选择了这样做,那么把视图层次的更视图设为view属性,你定义的实现不要调用super。
viewDidLoad
Called after the controller’s view is loaded into memory.
Discussion
This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView
method. You usually override this method to perform additional initialization on views that were loaded from nib files.
视图控制器把类层次加载到内存后,这个方法就会被调用。无论类层次从nib加载,还是通过load方法创建,这个方法都会被创建。
Responding to View Events
– viewWillAppear:
– viewDidAppear:
– viewWillDisappear:
– viewDidDisappear:
– viewWillLayoutSubviews
– viewDidLayoutSubviews
viewWillAppear:
Notifies the view controller that its view is about to be added to a view hierarchy.
Parameters
-
animated
-
If
YES
, the view is being added to the window using an animation.
Discussion
This method is called before the receiver’s view is about to be added to a view hierarchy and before any animations are
configured for showing the view. You can override this method to perform custom tasks associated with displaying the view.
For example, you might use this method to change the orientation or style of the status bar to coordinate with the orientation
or style of the view being presented. If you override this method, you must call super
at some point in your implementation.
For more information about the how views are added to view hierarchies by a view controller,
and the sequence of messages that occur, see “Responding to Display-Related Notifications”.
viewDidAppear:
Notifies the view controller that its view was added to a view hierarchy.
Parameters
-
animated
-
If
YES
, the view was added to the window using an animation.
Discussion
You can override this method to perform additional tasks associated with presenting the view.
If you override this method, you must call super
at some point in your implementation.
viewWillDisappear:
Notifies the view controller that its view is about to be removed from a view hierarchy.
Parameters
-
animated
-
If
YES
, the disappearance of the view is being animated.
Discussion
This method is called in response to a view being removed from a view hierarchy.
This method is called before the view is actually removed and before any animations are configured.
Subclasses can override this method and use it to commit editing changes, resign the first responder status of the view,
or perform other relevant tasks. For example, you might use this method to revert changes to the orientation
or style of the status bar that were made in the viewDidDisappear:
method when the view was first presented.
If you override this method, you must call super
at some point in your implementation.
viewWillLayoutSubviews
Called to notify the view controller that its view is about to layout its subviews.
Discussion
When a view’s bounds change, the view adjusts the position of its subviews.
Your view controller can override this method to make changes before the view lays out its subviews.
The default implementation of this method does nothing.
Availability
- Available in iOS 5.0 and later.
Declared In
UIViewController.h
viewDidLayoutSubviews
Called to notify the view controller that its view has just laid out its subviews.
Discussion
When the bounds change for a view controller’s view, the view adjusts the positions of its subviews
and then the system calls this method. However, this method being called does not indicate that
the individual layouts of the view’s subviews have been adjusted. Each subview is responsible for adjusting its own layout.
Your view controller can override this method to make changes after the view lays out its subviews.
The default implementation of this method does nothing.
Availability
- Available in iOS 5.0 and later.
Declared In
UIViewController.h
和前一个差不多。