UIViewController Class 参考2(Managing the View,Responding to View Events)

Managing the View


view

The view that the controller manages.

@property(nonatomic, retain)  UIView *view
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.

- (void)loadView
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.

- (void)viewDidLoad
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:

Notifies the view controller that its view is about to be added to a view hierarchy.

- (void)viewWillAppear:(BOOL) animated
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”.


详细说明:

在接受者的视图将要被添加到一个类层次之前,以及动态地显示视图之前,这个方法被调用。你可以override这个方法执行显示视图相关的自定义的任务。例如,
你可以使用这个方法去改变方法或者状态栏的风格,或者调整方向,或者视图被展示的风格。如果你override这个方法,你必须调用父类的方法。

如果你想了解更多关于视图控制器如何把视图添加到类层次中,以及消息的序列,参考   “Responding to Display-Related Notifications” .


viewDidAppear:

Notifies the view controller that its view was added to a view hierarchy.

- (void)viewDidAppear:(BOOL) animated
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.

Note: If a view controller is presented by a view controller inside of a popover, 

this method is not invoked on the presenting view controller after the presented controller is dismissed.

很简单,不翻译了

viewWillDisappear:

Notifies the view controller that its view is about to be removed from a view hierarchy.

- (void)viewWillDisappear:(BOOL) animated
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.

详细讨论:
当视图将要被从视图层次中去除时,方法被调用。子类可以override这个方法,然后用它来提交编辑变化,放弃视图的第一响应状态,或者执行其他相关的任务
,例如改变方向或者状态栏风格。如果override这个方法,你必须调用super.

viewWillLayoutSubviews

Called to notify the view controller that its view is about to layout its subviews.

- (void)viewWillLayoutSubviews
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

详细说明:
当视图的边界变化,视图调整子视图的位置。在视图布局子视图之前,你的视图可以override这个方法做出改变。默认不实现任何东西。

viewDidLayoutSubviews

Called to notify the view controller that its view has just laid out its subviews.

- (void)viewDidLayoutSubviews
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.
Related Sample Code
Declared In
UIViewController.h

和前一个差不多。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值