您的位置:首页 > 移动开发 > IOS开发

【Xcode6-Beta5】IOS静态库的制作与使用

2015-09-15 17:45 483 查看
一、制作静态库

1、创建静态库的Project

【1】File->New->Project->IOS->Framework & Library->Cocoa Touch Static Libary



【2】Product Name==>我的是MyAlertView->然后Create



【目录结构如图所示】会生成MyAlertView.h和MyAlertView.m



2、修改代码

【1】MyAlertView.h

[objc] view
plaincopy

//  

//  MyAlertView.h  

//  MyAlertView  

//  

//  Created by dagger on 14-8-14.  

//  Copyright (c) 2014年 Baidu. All rights reserved.  

//  

  

#import <Foundation/Foundation.h>  

#import "UIKit/UIKit.h"  

@interface MyAlertView : UIAlertView<UIAlertViewDelegate>  

  

+(void)showMessage:(NSString*)messageString;  

  

@end  

【2】MyAlertView.m

[objc] view
plaincopy

//  

//  MyAlertView.m  

//  MyAlertView  

//  

//  Created by dagger on 14-8-14.  

//  Copyright (c) 2014年 Baidu. All rights reserved.  

//  

  

#import "MyAlertView.h"  

  

@implementation MyAlertView  

  

+ (void)showMessage:(NSString*)messageString  

{  

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil  

                                                        message:messageString  

                                                       delegate:nil  

                                              cancelButtonTitle:@"OK"  

                                              otherButtonTitles:nil];  

    [alertView show];  

}  

  

@end  

 

3、设置Release



【1】Edit Scheme->Run->Release->Close



4、Build一下,发现Build Succeeded之后在工程目录下的Products文件夹下的静态库文件libMyAlertView.a依然为红色,右键选择【Show in Finder】也是进不去,此时我们可以单击静态库文件libMyAlertView.a会在Xcode最右边的子窗口中发现Full Path,按照完整路径我们可以找到静态库所在位置,如下图,我们在Finder下通过快捷键【command+shift+G】的方式可以输入文件路径前缀,然后一直找到静态库和对应所需的头文件







二、使用静态库

1、创建我的App的Project

【1】File->New->Project->IOS->Application->Single View Application



【2】Product Name==>我的是TestDemo->然后Create



【目录结构如图所示】



2、加入静态库

【1】向新建项目Project中添加静态库,如图,需要将include文件夹和静态库文件一并拖入到工程的TestDemo目录下



【2】在弹出框中勾选上【Copy items if needed】->【Finish】



【3】修改AppDelegate.m

    ​1)添加头文件:#import "include/MyAlertView/MyAlertView.h"

    ​2)  修改函数application(),添加如下代码[MyAlertView showMessage:@"This is a test!"];



3、Build之后可以看到使用成功的效果

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios