Bundle作用及使用方法

2021-07-05 12:25:54 字數 1762 閱讀 3006

bundle是乙個目錄,其中包含了程式會使用到的資源. 這些資源包含了如影象,聲音,編譯好的**,nib檔案(使用者也會把bundle稱為plug-in). 對應bundle,cocoa提供了類nsbundle.

我們的程式是乙個bundle. 在finder中,乙個應用程式看上去和其他檔案沒有什麼區別. 但是實際上它是乙個包含了nib檔案,編譯**,以及其他資源的目錄. 我們把這個目錄叫做程式的main bundle

bundle中的有些資源可以本地化.例如,對於foo.nib,我們可以有兩個版本: 乙個針對英語使用者,乙個針對法語使用者.  在bundle中就會有兩個子目錄:english.lproj和french.lproj,我們把各自版本的foo.nib檔案放到其中. 當程式需要載入foo.nib檔案時,bundle會自動根據所設定的語言來載入. 

通過使用下面的方法得到程式的main bundle

nsbundle *mybundle = [nsbundle mainbundle];

一般我們通過這種方法來得到bundle.如果你需要其他目錄的資源,可以指定路徑來取得bundle

nsbundle *goodbundle;

一旦我們有了nsbundle 物件,那麼就可以訪問其中的資源了

// extension is optional

nsstring *path = [goodbundle pathforimageresource:@"mom"];

nsimage *momphoto = [[nsimage alloc] initwithcontentsoffile:path];

bundle中可以包含乙個庫. 如果我們從庫得到乙個class, bundle會連線庫,並查詢該類:

class newclass = [goodbundle classnamed:@"rover"];

id newinstance = [[newclass alloc] init];

如果不知到class名,也可以通過查詢主要類來取得

class aclass = [goodbundle principalclass];

id aninstance = [[aclass alloc] init];

可以看到, nsbundle有很多的用途.在這章中, nsbundle負責(在後台)載入nib檔案. 我們也可以不通過nswindowcontroller來載入nib檔案, 直接使用nsbundle:

bool successful = [nsbundle loadnibnamed:@"about" owner:someobject];

注意噢, 我們指定了乙個物件someobject作為nib的file''s owner

//    通過使用下面的方法得到程式的main bundle

nsbundle *mainbundle = [nsbundle mainbundle];  

nsstring *imagepath = [mainbundle pathforresource:@"qq20120616-1"

oftype:@

"png"

];  

nslog(@"%@"

, imagepath);  

uiimage *image = [[uiimage alloc]initwithcontentsoffile:imagepath];  

uiimageview  *imageview = [[uiimageview alloc] initwithimage:image];   

[self.view addsubview:imageview];  

nmap 使用方法 及作用

檢視linux伺服器開啟了哪些埠和服務最好使用netstat命令,但是有些時候不知道開啟的埠到底使用了什麼服務,可以使用nmap來掃瞄,nmap軟體包需要安裝如果是red hat版本nmap包一般是預設安裝的,一下是nmap的使用方法。ping掃瞄 ping sweeping 埠掃瞄 port sc...

FactoryBean的使用方法及作用

自定義myfactorybean,只需要實現factorybean介面即可 package com.jack.factorybean component public class myfactorybean implements factorybean override public class g...

after和 before的作用及使用方法

1.before 和 after 的主要作用是在元素內容前後加上指定內容,示例 html p 你好 p css p beforep after效果如圖 以上 是 before和 after的基本用法,但是這兩種偽類還有很多更方便的用法。2.after清除浮動 元素設定浮動以後,其父元素以及父元素的兄...