Transferred from: http://www.cnblogs.com/ios8/p/ios-static-a.html
Due to the extreme scarcity of iphone controls and the limitations of custom components in reuse, we have accumulated a large number of "pure code" components in past projects--because of the limitations of IB itself, we cannot encapsulate these components as IB component libraries (we wanted to reuse these components by distributing xib files. But it turned out to be impossible at all, and Apple's plug-in programming did not support the iphone.
Finally we thought of the static library. While this is still a relatively primitive way to reuse, at least we can hide the source code of the component.
Below, we use the iphone static library to further encapsulate the custom component Checkbutton. (Implementation of the component refer to the previous post, "Customizing the Control check box and the implementation of the Radio box")
First, the implementation of static library
New project, select "Cocoa Touch Static Library" under the library. Give the project a name, for example: Yhylibrary.
Copy the 4 source files for the Checkbutton component: CheckButton.h, CHECKBUTTON.M, RadioGroup.h, radiogroup.m to classes directory, and Checkbutton 4 resource files: check.png, Uncheck.png, Radio.png, unradio.png, copy to the project folder.
Press down? +b compile, produce a. A file in the products directory.
Second, the new resource bundle
The static library does not contain resource files, although we have copied 4 resource files (. png files) to the static library project, but in fact these. png is not added to target, that is, the compilation results do not contain these resources, so if the static library is called at this time, all resources (string, IMAGES) are missing.
We can build the resources into separate bundles (bundles).
New Project "Mac OS X, Framework & Library Bundle" named: Yhylibrarybundle.
Then copy the above 4. png files into the resouces. Compile, generate the Yhylibrarybundle.bundle file.
Return to the static library project and create a new class: Utils.
Edit 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);
Edit 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;
}
The function Getmybundlepath can obtain the absolute file path of a specific resource in the bundle Yhylibrarybundle, such as:
/users/kmyhy/library/application Support/iphone simulator/4.2/applications/8213652f-a47e-456a-a7bb-4cd40892b66d/ Yhylibtest.app/yhylibrarybundle.bundle/contents/resources/radio.png
At the same time, modify the code in the CHECKBUTTON.M, import the Utils.h header file, the code which gets the picture is changed from imagenamed to Imagewithcontentsoffile, such as:
[Icon setimage: [UIImage Imagewithcontentsoffile:getmybundlepath (CheckName)];
That is, the image resource is read through an absolute path.
In addition to this approach, there is an easy way to copy the 4 resource files directly to the application project where you call the static library (no need to modify the static library code).
Third, Static library call
1. Add a static library
New Window-based Application project, named for the project, such as Yhylibrarytest.
Right-click Frameworks->add->existing Files. , add the Yhylibrary.xcodeproj file of the static library project to the current project (do not select Copy items).
Select the add in Yhylibrary.xcodeproj file, tick the "include to target" option, for example, make the last tick:
2. Add direct Dependencies (ie reference project)
Similar to the reference project in Visual Studio, the purpose is to make it easy to edit the referenced static library project directly in this project in order to modify the static library.
In the "Targets" directory, select "Firstlibrarytest", click the "Info" button, bring up the target's Properties window, switch to the "General" bar, click "Direct Dependencies" under the "+" button, Add the Engineering Static library libyhylibrary to the direct dependencies, as shown in the following:
3. Add header File Search path
Open the project's Info window, locate the header Search Paths in the Build column, and add the string ". /yhylibrary ".
4. Referencing resource bundles
Right-click on the copy bundle resources of Target and select "Add->existing File ..." To add the previously generated Yhylibrarybundle.bundle bundle to the project.
5. Calling classes in a static library
Edit Application: ( UIApplication *) Application didfinishlaunchingwithoptions: The code in the method:
radio Button Group
Radiogroup * RG =[[Radiogroup alloc] init];
1th radio button
Checkbutton * cb=[[Checkbutton alloc] Initwithframe:cgrectmake (20, 60, 260, 32)];
Add a radio button to a group of buttons
[RG ADD:CB];
Cb. Label. Text = @ "★";
Cb. Value =[[NSNumber alloc] initwithint:1];
Set the button to a radio button style
Cb. style = Checkbuttonstyleradio;
Join view
[Self. window ADDSUBVIEW:CB];
[CB release]; Add, will automatically hold, can release
2nd radio button
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];
3rd radio button
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];
The results of the operation are as follows:
6. Distribute the static library
Package and distribute the generated. a files and. bundle files to other people.
iOS development----generate static libraries (. a)