IPhone static libraryApplication EncapsulationWidgetLibrary tutorial is the content of this article, becauseIPhone controlsThe extreme lack of custom components and restrictions on reuse, we have accumulated a lot of "pure code" components in past projects-due to the limitations of IB itself, we cannot encapsulate these components as the IB component library. We originally wanted to reuse these components by distributing xib files, but ultimately we found this was impossible. Apple's Plug-in programming is not supported.IPhone).
Finally, we thoughtStatic Library. Although this is still a primitive method of reuse, we can at least hide the source code of the component. Below, we useIPhone static libraryFurther encapsulate the custom component CheckButton. For component implementation, refer to the previous blog post "CustomWidgetCheck box and single-Region Implementation)
1. Implement static databases
Create a project and select "Cocoa Touch Static Library" under the Library ". Name the project, for example, yhyLibrary.
Copy the four source files of the CheckButton component: CheckButton. h. CheckButton. m, RadioGroup. h. RadioGroup. m to the Classes directory, and copy the four resource files of the CheckButton: check.png?uncheck.png=radio.png=unradio.png to the project folder.
Press compile + B to compile. a. a file is generated under the Products directory.
Ii. Create a resource bundle
Static LibraryCan not include resource files, although we have already copied 4 resource file. PNG files)Static LibraryIn the project, the. PNG file on the worker will not be added to the target, that is, the compilation result does not contain these resources. Therefore, if you callStatic LibraryAll resource strings and images are missing.
We can build resources into separate Bundle ).
Create a project named "Mac OS X-> Framework & Library-> Bundle": yhyLibraryBundle.
Then copy 4. PNG files to Resouces. Compile and generate the yhyLibraryBundle. bundle file.
Return to the static library project and create a 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 getMyBundlePath function can obtain the absolute file path of a specific resource in the bundle yhyLibraryBundle, for example:
- /Users/kmyhy/Library/Application Support/iPhone Simulator/4.2/Applications/8213652F-A47E-456A-A7BB-4CD40892B66D/yhyLibTest.app/
- yhyLibraryBundle.bundle/Contents/Resources/radio.png
Modify the code in CheckButton. m, import the Utils. h header file, and change the image retrieval code from imageNamed to imageWithContentsOfFile, for example:
- [ icon setImage :[ UIImage imageWithContentsOfFile : getMyBundlePath ( checkname )]];
That is, the image resource is read through the absolute path.
In addition to this method, we can also simply copy four resource files directly to the application project that calls the static library without modifying the static library code ).
Iii. Static Library call
1. Add a static library
Create a Window-based Application project and name the project, such as yhyLibraryTest.
Right-click Frameworks-> Add-> Existing Files... and Add the yhyLibrary. xcodeproj file of the static library project to the current project. Do not select Copy items ).
Select the added yhyLibrary. xcodeproj file and check the "include to target" option. For example, set the last check box:
2. Add Direct Dependencies to reference the project)
Similar to the reference project in Visual Studio, the purpose is to directly edit the referenced static library project in this project, so as to modify the static library.
Select "FirstLibraryTest" under the "Targets" directory and click "info" to bring up the target property window and switch to the "General" column, click the "+" button under "Direct Dependencies" to add the static project library libyhyLibrary to Direct Dependencies. The result is as follows:
3. Add a header file search path
Open the project info window, find the Header Search Paths in the Build column, and add the string "../yhyLibrary ".
4. Reference a resource bundle
Right-click Copy Bundle Resources of target and choose Add-> Existing File ...", Add the yhyLibraryBundle. bundle generated earlier to the project.
5. Call classes in the static library
Edit application :( UIApplication *) application didfinishlaunchingwitexceptions: code in the method:
- // Single-choice button group
- RadioGroup * rg = [[RadioGroup alloc] init];
- // 1st radio buttons
- CheckButton * cb = [[CheckButton alloc] initWithFrame: CGRectMake (20, 60,260, 32)];
- // Add the single-choice button to the button group
- [Rg add: cb];
- Cb. label. text = @"★";
- Cb. value = [[NSNumber alloc] initWithInt: 1];
- // Set the button as a single-choice button style
- Cb. style = CheckButtonStyleRadio;
- // Add View
- [Self. window addSubview: cb];
- [Cb release]; // after adding, it is automatically held and can be released
- // 2nd radio buttons
- 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 buttons
- 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 running result is as follows:
6. Distribution of static databases
Package the generated. a and. bundle files and distribute them to others.
Summary:IPhone static libraryApplication EncapsulationWidgetI hope this article will be helpful to you!