由於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:
#define MYBUNDLE_NAME @ "yhyLibraryBundle.bundle"
#define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]
#define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]
NSString * getMyBundlePath( NSString * filename);
編輯Utils.m:
#import "Utils.h"
NSString* getMyBundlePath( NSString * filename)
{
NSBundle * libBundle = MYBUNDLE ;
if ( libBundle && filename ){
NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename];
NSLog ( @"%@" ,s);
return s;
}
return nil ;
}
函數getMyBundlePath可以取得束yhyLibraryBundle中具體資源的絕對文件路徑,如:
/Users/kmyhy/Library/Application Support/iPhone Simulator/4.2/Applications/8213652F-A47E-456A-A7BB-4CD40892B66D/yhyLibTest.app/yhyLibraryBundle.bundle/Contents/Resources/radio.png
同時,修改CheckButton.m中的代碼,導入Utils.h頭文件,把其中獲取圖片的代碼由imageNamed修改為imageWithContentsOfFile,如:
[ 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: 方法中的代碼:
// 單選按鈕組
RadioGroup * rg =[[ RadioGroup alloc ] init ];
// 第 1 個單選按鈕
CheckButton * cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 60 , 260 , 32 )];
// 把單選按鈕加入按鈕組
[ rg add :cb];
cb. label . text = @"★" ;
cb. value =[[ NSNumber alloc ] initWithInt : 1 ];
// 把按鈕設置為單選按鈕樣式
cb. style = CheckButtonStyleRadio ;
// 加入視圖
[ self . window addSubview :cb];
[cb release ]; //add 后,會自動持有,可以釋放
// 第 2 個單選按鈕
cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 100 , 260 , 32 )];
[ rg add :cb];
cb. label . text = @"★★" ;
cb. value =[[ NSNumber alloc ] initWithInt : 2 ];
cb. style = CheckButtonStyleRadio ;
[ self . window addSubview :cb];
[cb release ];
// 第 3 個單選按鈕
cb=[[ CheckButton alloc ] initWithFrame : CGRectMake ( 20 , 140 , 260 , 32 )];
[ rg add :cb];
cb. label . text = @"★★★" ;
cb. value =[[ NSNumber alloc ] initWithInt : 3 ];
cb. style = CheckButtonStyleRadio ;
[ self . window addSubview :cb];
[cb release ];
運行結果如下:
6、分發靜態庫
將生成的.a文件和.bundle文件打包分發給其他人。