您的位置:首页 > 产品设计 > UI/UE

UIViewController Class 参考1(Creating a View Controller Using Nib Files,Handling Memory Warnings)

2014-01-10 22:59 561 查看






Creating a View Controller Using Nib Files


initWithNibName:bundle:

Returns a newly initialized view controller with the nib file in the specified bundle.

从指定的bundle中通过nib文件得到新的初始化的视图控制器

- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle


Parameters


nibName

The name of the nib file to associate with the view controller. The nib file name should not contain any leading path information. If you specify 
nil
,
the 
nibName
 property
is set to 
nil
.

nibBundle

The bundle in which to search for the nib file. This method looks for the nib file in the bundle's language-specific project directories first, followed by the 
Resources
 directory.
If this parameter is 
nil
, the method uses the heuristics described below to locate the nib file.

参数

nibName


和视图控制器相关的nib文件的名字。nib文件不应该包含任何主要的路径信息。如果你指定为nil,那么 nibName属性被设置为nil.

nibBundle

系统将在这个bundle中寻找nib文件。这个方法首先在bundle指定语音的项目目录中寻找 bundle文件。然后在Resources目录找。如果这个参数为nil,那它会用下面说明的启发式算法锁定nib文件


Return Value


A newly initialized 
UIViewController
 object.


Discussion


This is the designated initializer for this class.

The nib file you specify is not loaded right away. It is loaded the first time the view controller’s view is accessed. If you want to perform additional initialization after the nib file is loaded, override the 
viewDidLoad
 method
and perform your tasks there.

If you specify 
nil
 for the nibName parameter and you do not override the 
loadView
 method,
the view controller searches for a nib file using other means. See 
nibName
.

If your app uses a storyboard to define a view controller and its associated views, your app never initializes objects of that class directly. Instead, view controllers are either instantiated by the storyboard—either automatically by iOS when a segue is triggered
or programmatically when your app calls the storyboard object’s 
instantiateViewControllerWithIdentifier:
 method.
When instantiating a view controller from a storyboard, iOS initializes the new view controller by calling its 
initWithCoder:
 method
instead. iOS automatically sets the 
nibName
 property
to a nib file stored inside the storyboard.

For more information about how a view controller loads its view, see “Resource
Management in View Controllers”.
用法:

这是这个类指定的初始化方法。
指定的nib文件并不会被立刻加载。第一次获取视图控制器的视图时才加载。如果你想在nib文件被加载后执行额外的初始化内容,可以override viewDidLoad方法,在那里执行任务。

如果你把nibName参数指定为nil,并且你没有override loadView方法,那么视图控制器使用其他方法,参考 nibName属性。

如果你的应用使用storyboard定义视图控制以及与其相关的类,你不需要直接实例类的对象。事实上,这个时候视图控制器要么被storyboard实例化,要么在segue被触发的时候被iOS系统实例化,或者当你的app调用storyboard 对象的 instantiateViewControllerWithIdentifer:方法时被实例化。从storyboard实例化时,iOS通过调用initWithCoder:方法实例新的视图控制器。iOS自动设置nibName属性为存在storyboard的nib文件。

如果想详细了解视图控制器如何加载它的视图,参考 “Resource
Management in View Controllers”.


nibName

Return the name of the receiver’s nib file, if one was specified. (read-only)

如果指定了一个nib文件,返回接受者的nib文件的名字。(只读)

@property(nonatomic, readonly, copy) NSString *nibName


Discussion


This property contains the value specified at initialization time to the 
initWithNibName:bundle:
 method. The value of this property may be 
nil
.

If you use a nib file to store your view controller’s view, it is recommended that you specify that nib file explicitly when initializing your view controller. However, if you do not specify a nib name, and do not override the 
loadView 
 method
in your custom subclass, the view controller searches for a nib file using other means. Specifically, it looks for a nib file with an appropriate name (without the 
.nib
 extension)
and loads that nib file whenever its view is requested. Specifically, it looks (in order) for a nib file with one of the following names:

If the view controller class name ends with the word “Controller”, as in 
MyViewController
, it looks for a nib file whose name matches the class
name without the word “Controller”, as in 
MyView.nib
.

It looks for a nib file whose name matches the name of the view controller class. For example, if the class name is 
MyViewController
, it looks
for a 
MyViewController.nib
 file.

详细描述:

这个属性包含了在初始化时在initWithNibName:bundle中指定的值。这个属性的值可能时nil。

如果你使用nib文件去存储你的视图控制器的视图,推荐初始化视图控制器时显式指定nib 文件。当然,如果你没有指定nib名字,并且没有在自定义子类中overrode loadView方法,视图控制器用其他方法搜索nib文件。特别地,它用适当的名字寻找nib文件。并且每当视图被请求时就加载那个nib文件。特别地,它以下面的一种名字顺序地寻找nib文件。
1,如果视图控制器类的名字以"Controller"结尾,例如MyViewController,它通过不带"Controller"和类名匹配搜索,比如MyView.nib.
2, 通过匹配 视图控制的名字搜索nib文件,例如 ,如果类名是 MyViewController,它寻找MyViewController.nib

Note: Nib names that include a platform-specific identifier such as 
~iphone
 or 
~ipad
 are
loaded only on a device of the corresponding type. For example, a nib name of 
MyViewController~ipad.nib
 is loaded only on iPad. If your app
supports both platform types, you must provide versions of your nib files for each platform.

注意:如果Nib名字包含了指定平台的标示符,例如 ~iphone 或者 ~ipad ,那么根据相应的类型加载到一个设备中。




Handling Memory Warnings

– didReceiveMemoryWarning



didReceiveMemoryWarning

Sent to the view controller when the app receives a memory warning.

- (void)didReceiveMemoryWarning


Discussion


Your app never calls this method directly. Instead, this method is called when the system determines that the amount of available memory is low.

You can override this method to release any additional memory used by your view controller. 

If you do, your implementation of this method must call the 
super
 implementation
at some point.

详细描述:

你的应用永远不要直接调用这个
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uiviewcontroller