xib 文件的實質是 xml,用來描述界面對象,每個對象都有一個很重要的屬性——identity inspector 面板中的 class 屬性。加載 xib 文件的時候,實際上就是實例化界面對象相對應的這些 class。
xib 文件的加載過程:
1. 將 xib 文件從磁盤載入內存,有兩種技術可以加載 xib 文件:NSBundle 和 UINib。
2. 執行 unarchive 和 initialize 操作,該過程主要由 NSCoding Protocol 中的 initWithCoder:(NSCoder *)decoder 完成。
3. 建立 connections:Outlets 和 Actions。Outlets 使用 IBOutlet 關鍵字標示,使用 setValue:forKey: 方法建立每個 Outlet,所以每個 Outlet 的建立都會發送 KVO 通知。Actions 使用 IBAction 關鍵字標示,替換 void 返回值,通過調用 addTarget:action:forControlEvents: 方法建立每個 Action 連接。注意,這裡構建 Outlets 和 Actions 是有先後順序的:先建立 Outlets 連接,隨後建立 Actions 連接。因為 Actions 的建立依賴之前建立的 Outlets。
4. 調用 awakeFromNib 方法。首先要調用 super 的 awakeFromNib 方法,之後可以設置一些個性化的操作,以及一些無法在設計時設定的操作。注意,awakeFromNib 消息只發往在 Interface Builder 中指定的 Custom Class,不會發送給 File's Owner、First Responder 等占位對象。
之後,該對象的加載完成,可以進行各式各樣的操作了。
使用 NSBundle 加載 xib 文件:
[[NSBundle mainBundle] loadNibNamed:<(NSString *)> owner:<(id)> options:<(NSDictionary *)>];
這是最常見的一種。loadNibNamed:owner:options: 方法返回的是一個 NSArray*,裡面包含了所加載 xib 文件包含的界面對象(class)。
NSBundle 每次都從磁盤上載入 xib 文件,而 UINib 則只是第一次從磁盤上載入 xib 文件,之後將 xib 文件緩存在內存中。每次新生成一個對象時,直接訪問內存中的 xib 文件執行上面描述的 2-4 步,所以性能上會有很大的提升。並且開發者文檔也建議,對於那些重複使用的 xib 文件使用 UINib 加載技術;當收到低內存警告時,會從內存中卸載 xib 文件,再次訪問時會從磁盤中載入。下面看一下 UINib 的定義:
NS_CLASS_AVAILABLE_IOS(4_0) @interface UINib : NSObject { @private id storage; } // If the bundle parameter is nil, the main bundle is used. // Releases resources in response to memory pressure (e.g. memory warning), reloading from the bundle when necessary. + (UINib *)nibWithNibName:(NSString *)name bundle:(NSBundle *)bundleOrNil; // If the bundle parameter is nil, the main bundle is used. + (UINib *)nibWithData:(NSData *)data bundle:(NSBundle *)bundleOrNil; // Returns an array containing the top-level objects from the NIB. // The owner and options parameters may both be nil. // If the owner parameter is nil, connections to File's Owner are not permitted. // Options are identical to the options specified with -[NSBundle loadNibNamed:owner:options:] - (NSArray *)instantiateWithOwner:(id)ownerOrNil options:(NSDictionary *)optionsOrNil; @end
前面兩個方法很清楚,分別以不同的方式載入;第三個方法則是實例化(將對象創建出來)。
表視圖實例:
具體細節就不多說了。創建一個名為 Empty 的 xib 文件。
注意看 Table View Cell 的 class 屬性是 TableViewCell,不是默認的 UITableViewCell。TableViewCell.h 如下:
@interface TableViewCell : UITableViewCell @property (weak, nonatomic) IBOutlet UILabel *lb1; @property (weak, nonatomic) IBOutlet UILabel *lb2; - (IBAction)bt:(id)sender; @end
三個屬性都和 xib 文件進行了鏈接,應該能看出來。
然後在 UITableViewDataSource 代理中分別進行如下操作:
//頭文件內聲明 UINib *nib; //實例化 self->nib = [UINib nibWithNibName:@"Empty" bundle:nil];
然後來看 tableView:cellForRowAtIndexPath: 方法,這個方法標準的實現方法如下:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = [NSString stringWithFormat:@"Cell"]; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; } //config the cell return cell; }
這是使用代碼的方式創建 cell,下面看使用 UINib:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *identifier = @"Cell"; TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; if (cell == nil) { cell = [[nib instantiateWithOwner:nil options:nil] objectAtIndex:0]; } switch (indexPath.section) { case redSection: cell.lb1.text = @"lb1"; cell.lb2.text = @"lb2"; break; case blueSection: break; default: [[cell textLabel] setText:@"Unknow"]; } return cell; }