如何在静态库中使用XIB等资源

来源:互联网 发布:清华大学网络教育招生 编辑:程序博客网 时间:2024/06/05 10:21

注:CSDN的BLOG显示的图不完整,可以在图上右键->查看图像看到完整图片


因为项目代码越来越多,所以最近想把项目拆分成几个项目,就像VS里的方案一样,本以为是很容易的事情,结果在XCODE里那是相当麻烦

不得不说APPLE的XCODE产品经理绝对是无语啊...还是喜欢VS的IDE

OK说正题

本文主要是从

http://iphone.galloway.me.uk/iphone-sdktutorials/ios-library-with-resources/

整理翻译过来的.

解决

1,如何用静态库并且能联调

2,如何在使用静态库中资源

Step 1: Create the library project

First of all you need to create a library project which will be a standard Cocoa Touch static library first of all. So go ahead and create a new project from the “Cocoa Touch Static Library” template as shown in the following two screenshots.


第一步:创建静态库项目

新建项目->IOS->Framework & Library->Cocoa Touch Static Library

Step 2: Adding a copy files phase

In order for the headers from the static library to be picked up by a project which includes it, we need to add a copy files phase to the library to copy the headers to a certain directory. From a bit of trial and error and some other blog posts I found that we need to put these into the “Products Directory” under a subpath of include.

To add the phase you need to go to the build settings of the library project and under the “Build Phases” tab you’ll see a button called “Add Build Phase”. Click that and set it up as per the screenshots below.

第二步:添加需要复制出去的文件

简单来说,要把需要引用的类的.H文件复制到一个公共的目录,便于其它项目访问.

在这里要说明下,原作者创建静态库似乎默认就有个MyViewController类,但我新建时是没有的,所以需要手动新建一个测试用的类.建议使用UIViewController

选择MyLibrary项目,File->New->New File->Cocoa Touch->UIViewController subclass,名称就叫MyViewController

再选择MyLibrary项目,然后在右边面板选择Build Phases,点下方Add  Build Phases,再点Add Copy Files

Destination选择Products Directory,subpath输入include


Step 3: Adding the resources bundle target

The magic for getting the resources to play nicely is to put all the resources into a bundle which we’ll include from another project. So we need to add a target to the library project of type “Bundle”. From the build settings of your library project click the “Add Target” button and use the screenshot below to help you set it up.

第三步:添加资源Bundle Target

资源必须是单独编译成Bundle才能使用,所以需要为MyLibrary项目生成另一个Bundle Target

选择MyLibrary项目,在右边面板中点Add Target->MAC OS X->FRAMEWORK &LIBRARY->Bundle

需要说明,bundle项目只有MAC OS X里才有,但实际上也是可以用于IOS的,只是需要做些改动


Step 4: Fixing the resources bundle target

When the resources bundle target is added, it will default to being set up with a Mac OS X build target. This is wrong because we want an iOS one so you just need to change the relevant settings to whatever you want your iOS target to be (probably “Latest iOS”). Use the screenshot below to guide you in doing this.

第四步:修改资源bundle target

把bundle target修改成ios能用的,看图

Build Settings设置

1)Architectures为Standard (armv7)

2)Build Active Architecture Only 为指定的IOS版本


Step 5: Adding a XIB to the bundle target

In order to get resources to be put into the bundle all you have to do is add them to the “Copy Bundle Resources” build phase of the target. The following screenshot shows an example ofMyViewController.xib being put into the bundle.

第五步:添加 XIB的输出

设置XIB为输出到BUNDLE,这一步相当于VS c#开发里把某个图片设置编译动作为嵌入资源.

选择Mylibrary项目,targets选择MyLibraryResources,Build Phases面板

Copy Bundle Resources点+选择xib文件


Step 6: Creating the app project

Now we’re ready to create an app which will use the library. So let’s go ahead and do that. Just create an empty application for now. The following screenshots show an example of doing that.

第6步:创建测试程序

现在创建一个测试程序来看看调用静态库里的MyViewController效果

Step 7: Linking with the library

In order to get the static library to be linked with the main app project you just need to drag the library project from Finder into the navigator pane in Xcode and drop it next to the project. It doesn’t really matter if it’s a sub project or a sibling project in a workspace – do whichever you feel most comfortable with.

Then we need to edit the app’s scheme to make it build the library project’s targets first. So edit the scheme and under the “Build” tab click the plus at the bottom and add the library target and library resources target. You should then have something which looks like the following screenshot.

第7步:关联静态库引用

这一步相当于VS C#开发中的项目引用

把主程序和静态库做关联,这样省得每次要手动添加静态库的.a文件

要说明下:我原本以为编译主程序会自动编译静态库,实际上做的并没有那么好,我改动了静态库的XIB内容,但编译主程序后发现内容还是上次的,所以我每次都是clean build.

编辑Scheme(菜单Product->Edit Scheme)

选择Build

点+,选择MyLibrary和MylibraryResources项目

Step 8: Linking against the library

We now need to tell the app project to link against the static library from the library project. To do this you just need to add it to the list of linked frameworks in the target. The following screenshot shows an example of doing this.

第八步:其实还是关联静态库引用

上一步是做了编译主程序自动编译静态库,这一步是要把静态库生成的.a文件动态引用到主程序里

选择MylibraryResources项目在右边Build Phases面板Link Binary WithLibraries点+,选择libMylibrary.a文件


We also need to add the bundle from the library project to the main app project. To do this we just need to drag it across from the navigator pane to the “Copy Bundle Resources” phase of the application project. Once done it should look something like the following screenshot.

现在需要把静态库中的bundle也与主程序做关联

还是在Build Phases面板

展开Copy Bundle Resources

要注意,这次不是点+了,需要拖拽Mylibrary项目中的xib文件到Copy Bundle Resources里


Step 9: Final setup of app project

The last bit to do is to set up the header search paths of the app project. Go to the build settings of the app project and look for “User Header Search Paths”. Then set the target setting to$(BUILT_PRODUCTS_DIR) so that it looks something like the following screenshot.

第九步:最后设置主程序项目

选择MyLibraryResource项目在Build Settings面板里

设置User Header Search Paths为$(BUILT_PRODUCTS_DIR),注意建议把Recursive勾上(递归搜索)



Step 10: Coding the example view controller

In my example I created a view controller called MyViewController with a XIB that gets put into the library bundle. The code for this just has to be a bit different to normal to pick up the XIB from our library bundle. So here is the code:

第10步:编写MyviewController代码

#import <UIKit/UIKit.h> @interface MyViewController : UIViewController @end @implementation MyViewController - (id)init {    NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"MyLibraryResources" withExtension:@"bundle"]];    if ((self = [super initWithNibName:@"MyViewController" bundle:bundle])) {    }    return self;} @end
其实主要就是这段代码:
  NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"MyLibraryResources" withExtension:@"bundle"]];

OK,基本上就是这样了.代码在原地址有,大家可以仔细看看.

原创粉丝点击