iOS Xcode制作靜態(tài)庫詳解
- #import <UIKit/UIKit.h>
- @interface MyView : UIView
- @end
創(chuàng)建視圖類的.m文件
- #import "MyView.h"
- @implementation MyView
- - (id)initWithFrame:(CGRect)frame
- {
- //初始化視圖位置
- self = [super initWithFrame:frame];
- if (self) {
- }
- return self;
- }
- - (void)drawRect:(CGRect)rect
- {
- //這里創(chuàng)建一個圖片視圖
- UIImage *image=[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com.hk/intl/zh-CN/images/logo_cn.png"]]];
- [image drawInRect:CGRectMake(0, 0,self.frame.size.width , self.frame.size.height)];
- [image release];
- }
- -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
- {
- //點擊視圖后打開網(wǎng)頁
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.baidu.com"]];
- }
- @end
到這一步靜態(tài)庫中的代碼我們已經寫完,現(xiàn)在我們開始制作靜態(tài)庫。靜態(tài)庫的制作方法可分為兩種:第一種為在真機上使用的靜態(tài)庫,第二種為在模擬器中使用的靜態(tài)庫。這兩種方法制作起來有點小區(qū)別,請大家一定看好了,別眨眼睛喔, 我們開始從模擬器入手。

找到文件以后,默認為Debug-iphoneos / libsdklib.a ,但是這個文件是不能在模擬器中使用的,并且它也不能在真機中使用。你需要選擇下面Debug-iphoneosimulator / libsdklib.a這個文件,將libSDKLib.a與對應MyView.h頭文件拖拽入使用它的工程當中。
接下來創(chuàng)建一個普通的IOS工程,先選擇模擬器運行 iPhone5.0 Simulator ,然后將libSDKLib.a 與MyView.h拖拽添加至程序當中即可。
運行工程即可看到效果,本例中通過URL加載了一張谷歌的LOGO,觸摸點擊該視圖后打開百度的首頁。
調用靜態(tài)庫的方法如下:
- #import "ViewController.h"
- #import "MyView.h"
- @implementation ViewController
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- #pragma mark - View lifecycle
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- //創(chuàng)建靜態(tài)庫視圖
- MyView *myView = [[MyView alloc] initWithFrame: CGRectMake(0, 0, 120, 100)];
- //將靜態(tài)庫視圖添加至窗口當中
- [self.view addSubview:myView];
- [myView release];
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- @end
OK ,到這一步我們已經將靜態(tài)庫順利的應用在模擬器當中,下面我們學習如何將靜態(tài)庫應用在真機當中。我們再次回到制作靜態(tài)庫的工程當中,如圖打開工程后編譯環(huán)境選擇IOS Device,然后構建構成即可完成。