NSBundle和XIB加载

NSBundle 类中,苹果给出的解释是:
An NSBundle object represents a location in the file system that groups code and resources that can be used in a program. NSBundle objects locate program resources, dynamically load and unload executable code, and assist in localization. You build a bundle in Xcode using one of these project types: Application, Framework, plug-ins.
大概翻译过来:
NSBundle 对象指代相应应用程序下的所有可用的文件系统。就是说,可以用NSBundle操作应用程序下,所有可用的资源(包括,xib文件,数据文件,图片 等)。
 
NSBundle 英语中的解释是:“捆,束”的意思,那我们可以理解为:
NSBundle是将程序中所有资源捆在一起的对象。
 

mainBundle方法:

Returns the NSBundle object that corresponds to the directory where the current application executable is located.

+ (NSBundle *)mainBundle

该方法:返回NSBundle 对象;可以用该对象来返回应用程序可操作的路径和文件。

NSBundle *myBundle = [NSBundle mainBundle];

已经包括了很多已经封装好的方法。

边学习,边了解把。

1、loadNibNamed方法:

- (NSArray *)loadNibNamed:(NSString *)name owner:(id)owner options:(NSDictionary *)options

参数:

name:nib文件的名称

owner:指定name参数所指代的nib文件的File's Owner

options:当nib文件开始时,需要的数据

返回值:返回符合对象的数组。

例子:初始化一个View

CustomCell *cell=(CustomCell *)[tableView dequeueReusableCellWithIdentifier:CustomCellTableIdentifier];

if (cell==nil) {

NSArray *nib=[[NSBundle mainBundle] loadNibNamed:@"CustomCell" owner:selfoptions:nil];

cell=[nib objectAtIndex:0];// 因为返回的是数组

}

 

小白的转变
原文地址:https://www.cnblogs.com/CCJ4EVER/p/5038205.html