版本記錄
| 版本號 | 時間 |
|---|---|
| V1.0 | 2018.04.30 |
前言
iOS中的視圖加載可以有兩種方式,一種是通過xib加載,另外一種就是通過純代碼加載。它們各有優(yōu)點和好處,xib比較直觀簡單,代碼比較靈活但是看著很多很亂,上一家公司主要風(fēng)格就是用純代碼,這一家用的就是xib用的比較多。這幾篇我們就詳細(xì)的介紹一個xib相關(guān)知識。感興趣的可以看上面寫的幾篇。
1. xib相關(guān)(一) —— 基本知識(一)
2. xib相關(guān)(二) —— 文件沖突問題(一)
3. xib相關(guān)(三) —— xib右側(cè)標(biāo)簽介紹(一)
4. xib相關(guān)(四) —— 連線問題(一)
5. xib相關(guān)(五) —— 利用layout進行約束之界面(一)
6. xib相關(guān)(六) —— 利用layout進行約束之說明和注意事項(二)
7. xib相關(guān)(七) —— Storyboard中的segue (一)
8. xib相關(guān)(八) —— Size Classes(一)
9. xib相關(guān)(九) —— 幾個IB修飾符(一)
10. xib相關(guān)(十) —— xib的國際化(一)
11. xib相關(guān)(十一) —— xib的高冷用法之修改視圖的圓角半徑、邊框?qū)挾群皖伾ㄒ唬?/a>
12. xib相關(guān)(十二) —— UIStackView之基本介紹(一)
13. xib相關(guān)(十三) —— UIStackView之枚舉UIStackViewDistribution使用(二)
14. xib相關(guān)(十四) —— UIStackView之UIStackViewAlignment使用(三)
15. xib相關(guān)(十五) —— UIStackView之工程實踐(四)
回顧
上一篇主要講述了UIStackView的工程實踐,這一篇主要講述UINib。
Overview

UINib對象在內(nèi)存中緩存nib文件的內(nèi)容,準(zhǔn)備進行解檔和實例化。當(dāng)您的應(yīng)用程序需要實例化nib文件的內(nèi)容時,它可以做到這一點,而無需首先從nib文件加載數(shù)據(jù),從而提高性能。 UINib對象可以在內(nèi)存不足的情況下自動釋放此緩存的nib數(shù)據(jù)以釋放內(nèi)存給應(yīng)用程序,并在下次應(yīng)用程序?qū)嵗痭ib時重新加載該數(shù)據(jù)。只要需要反復(fù)實例化相同的nib數(shù)據(jù),應(yīng)用程序就應(yīng)該使用UINib對象。例如,如果您的table view使用nib文件來實例化table view cell,則在UINib對象中緩存nib可以顯著提高性能。
當(dāng)您使用nib文件的內(nèi)容創(chuàng)建UINib對象時,該對象會將對象圖加載到引用的nib文件中,但尚未將其解壓縮。為了解開所有的nib數(shù)據(jù)并因此真正實例化nib,應(yīng)用程序會調(diào)用UINib對象上的instantiateWithOwner:options:方法。在Resource Programming Guide詳細(xì)的描述了UINib對象遵循以實例化該nib的對象圖的步驟。
Topics
1. Creating a Nib Object
-
- 在指定的bundle文件中,返回一個實例化nib文件的UINib文件。
-
- 從內(nèi)存中存儲的nib數(shù)據(jù)創(chuàng)建一個UINib對象。
2. Instantiating a Nib
-
- instantiateWithOwner:options:
- 解檔并實例化內(nèi)存中的nib文件,創(chuàng)建一個具體的對象樹并設(shè)置為頂部級別對象。
-
- 在iOS 2.x中,此鍵的值是一個字典,其中包含nib文件中使用的任何代理對象的運行時替換對象。 在這個字典中,鍵是與代理對象關(guān)聯(lián)的名稱,值是您的代碼中應(yīng)該使用的實際對象。
-
- 替換nib文件中的任何代理對象。 這個鍵的值是一個NSDictionary對象。 字典的鍵是nib文件中任何代理對象的名稱,每個鍵的值是代替代理使用的實際對象。
API - UINib
Xib其實就是固化在bundle里面的文件,每次加載都需要從Xib進行喚醒。
UINib就是封裝或包含Interface Builder nib文件的對象。
先看一下UINib的API
#import <Foundation/Foundation.h>
#import <UIKit/UIKitDefines.h>
NS_ASSUME_NONNULL_BEGIN
NS_CLASS_AVAILABLE_IOS(4_0) @interface UINib : NSObject
// 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:(nullable NSBundle *)bundleOrNil;
// If the bundle parameter is nil, the main bundle is used.
+ (UINib *)nibWithData:(NSData *)data bundle:(nullable 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:(nullable id)ownerOrNil options:(nullable NSDictionary *)optionsOrNil;
@end
NS_ASSUME_NONNULL_END
這里需要說明的是:
1. + (UINib *)nibWithNibName:(NSString *)name bundle:(nullable NSBundle *)bundleOrNil;
該方法的作用就是創(chuàng)建一個UINib對象。
如果bundle的參數(shù)是nil,那么就使用 main bundle,在內(nèi)存壓力(內(nèi)存警告)緊張的時候會釋放該資源,并根據(jù)需要從buudle加載數(shù)據(jù)。
2. + (UINib *)nibWithData:(NSData *)data bundle:(nullable NSBundle *)bundleOrNil;
該方法的作用就是創(chuàng)建一個UINib對象。
如果bundle的參數(shù)是nil,那么就使用 main bundle。
3. - (NSArray *)instantiateWithOwner:(nullable id)ownerOrNil options:(nullable NSDictionary *)optionsOrNil;
該方法的作用實例化UINib對象。
該方法返回一個從NIB中加載的包含頂部級別的對象數(shù)組,owner和options參數(shù)均可為nil,如果owner參數(shù)為nil,那么鏈接File's Owner就不被允許,Options與-[NSBundle loadNibNamed:owner:options:]指定的options的參數(shù)是一樣的。
后記
本篇主要講述了UINib相關(guān),感興趣的給個贊或者關(guān)注~~~~
