iPhone靜態庫應用之封裝控制項陳列庫教程

來源:互聯網
上載者:User

iPhone靜態庫應用之封裝控制項庫教程是本文要介紹的內容,由於iPhone控制項的極度匱乏和自訂群組件在重用上的限制,在過去的項目中我們積累了大量的“純程式碼”組件——因為IB本身的限制,我們無法把這些組件封裝為IB組件庫本來我們想通過分發xib檔案的方式重用這些組件,但最終發現這根本不可能,蘋果的Plug-in編程不支援iPhone)。

最終我們想到了靜態庫。雖然這仍然還是一種比較原始的複用方式,但起碼我們可以隱藏組件的原始碼。下面, 我們使用iPhone靜態庫把自訂群組件CheckButton 進行進一步的封裝。組件的實現參考前一篇博文《自訂控制項複選框和單選框的實現》)

一、實現靜態庫

建立工程, 選擇 Library 下的 “ Cocoa Touch Static Library ” 。給工程命名,例如:yhyLibrary。                 

複製CheckButton組件的4個源檔案:CheckButton.h、CheckButton.m、RadioGroup.h、RadioGroup.m到Classes目錄下,同時把CheckButton的4個資源檔:check.png、uncheck.png、radio.png、unradio.png,複製到工程檔案夾。

按下 ⌘ +b編譯,在Products目錄下即產生一個 .a檔案。

二、 建立資源束

靜態庫中並不能包含資源檔,雖然我們已經把4個資源檔.png檔案)拷貝到靜態庫工程中,但實際上這些.png是不會添加到target的,也就是說編譯結果中並不包含這些資源,因此如果此時調用靜態庫,所有的資源字串、圖片)都是缺失的。

我們可以把資源建立成單獨的束Bundle)。

建立工程“ Mac OS X  -> Framework & Library -> Bundle ”,命名為:yhyLibraryBundle。

然後把上面4個.png檔案拷進Resouces中去。編譯,產生yhyLibraryBundle.bundle檔案。

返回靜態庫工程,建立一個類:Utils 。

編輯Utils.h:

 
  1. #define MYBUNDLE_NAME @ "yhyLibraryBundle.bundle"   
  2. #define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]   
  3. #define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]   
  4. NSString * getMyBundlePath( NSString * filename);  

編輯Utils.m:

 
  1. #import "Utils.h"   
  2. NSString* getMyBundlePath( NSString * filename)   
  3. {   
  4. NSBundle * libBundle = MYBUNDLE ;   
  5. if ( libBundle && filename ){   
  6. NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename];   
  7. NSLog ( @"%@" ,s);   
  8. return s;   
  9. }   
  10. return nil ;   
  11. }  

函數getMyBundlePath可以取得束yhyLibraryBundle中具體資源的絕對檔案路徑,如:

 
  1. /Users/kmyhy/Library/Application Support/iPhone Simulator/4.2/Applications/8213652F-A47E-456A-A7BB-4CD40892B66D/yhyLibTest.app/
  2. yhyLibraryBundle.bundle/Contents/Resources/radio.png  

同時,修改CheckButton.m中的代碼,匯入Utils.h標頭檔,把其中擷取圖片的代碼由imageNamed修改為imageWithContentsOfFile,如:

 
  1. [ icon setImage :[ UIImage imageWithContentsOfFile : getMyBundlePath ( checkname )]];  

即通過絕對路徑讀取圖片資源。

除了這種方法,我們還可以有一個簡單辦法,就是把4個資源檔直接拷貝到你調用靜態庫的應用工程中不需要修改靜態庫代碼)。

三、靜態庫調用

1、添加靜態庫

建立Window-based Application工程,給工程命名,如yhyLibraryTest。

右鍵點 Frameworks->Add->Existing Files.. ,把靜態庫工程的yhyLibrary.xcodeproj檔案 添加到當前工程不要選擇 Copy items ) 。

選中添加進來的yhyLibrary.xcodeproj檔案,勾選“include to target”選項,如,打上最後一個小勾:

2、添加Direct Dependencies即引用工程)

類似於Visual Studio中的引用工程,目的是便於在本工程中直接編輯所引用的靜態庫工程,以便對靜態庫進行修改。

在“ Targets ”目錄下選擇“ FirstLibraryTest ”,點擊“info”按鈕,調出目標的屬性視窗,切換到“General”欄,點擊“ Direct Dependencies ”下方的“ + ”按鈕,將工程靜態庫libyhyLibrary添加到Direct Dependencies中,結果如:

3、添加標頭檔搜尋路徑

開啟工程的info視窗,在Build欄中找到Header Search Paths,添加字串“../yhyLibrary”。

4、 引用資源束

在target的Copy Bundle Resources上右鍵,選擇“Add->Existing File…”,把前面產生的yhyLibraryBundle.bundle束添加到工程。

5、調用靜態庫中的類

編輯 application:( UIApplication *)application didFinishLaunchingWithOptions: 方法中的代碼:

 
  1. // 選項按鈕組   
  2. RadioGroup * rg =[[ RadioGroup alloc ] init ];   
  3. // 第 1 個選項按鈕   
  4. CheckButton * cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 60 , 260 , 32 )];   
  5. // 把選項按鈕加入按鈕組   
  6. [ rg add :cb];   
  7. cb. label . text = @"★" ;   
  8. cb. value =[[ NSNumber alloc ] initWithInt : 1 ];   
  9. // 把按鈕設定為選項按鈕樣式   
  10. cb. style = CheckButtonStyleRadio ;   
  11. // 加入視圖   
  12. [ self . window addSubview :cb];   
  13. [cb release ]; //add 後,會自動持有,可以釋放   
  14. // 第 2 個選項按鈕   
  15. cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 100 , 260 , 32 )];   
  16. [ rg add :cb];   
  17. cb. label . text = @"★★" ;   
  18. cb. value =[[ NSNumber alloc ] initWithInt : 2 ];   
  19. cb. style = CheckButtonStyleRadio ;   
  20. [ self . window addSubview :cb];   
  21. [cb release ];   
  22. // 第 3 個選項按鈕   
  23. cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 140 , 260 , 32 )];   
  24. [ rg add :cb];   
  25. cb. label . text = @"★★★" ;   
  26. cb. value =[[ NSNumber alloc ] initWithInt : 3 ];   
  27. cb. style = CheckButtonStyleRadio ;   
  28. [ self . window addSubview :cb];   
  29. [cb release ];  

運行結果如下:

6、分發靜態庫

將產生的.a檔案和.bundle檔案打包分發給其他人。

小結:iPhone靜態庫應用之封裝控制項庫教程的內容介紹完了,希望本文對你有所協助!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.