IPhone static library application-encapsulation control library tutorial

Source: Internet
Author: User

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:

 
 
  1. #define MYBUNDLE_NAME @ "yhyLibraryBundle.bundle"   
  2. #define MYBUNDLE_PATH [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: MYBUNDLE_NAME]   
  3. #define MYBUNDLE [NSBundle bundleWithPath: MYBUNDLE_PATH]   
  4. NSString * getMyBundlePath( NSString * filename);  

Edit Utils. m:

 
 
  1. #import "Utils.h"   
  2. NSString* getMyBundlePath( NSString * filename)   
  3. {   
  4. NSBundle * libBundle = MYBUNDLE ;   
  5. if ( libBundle && filename ){   
  6. NSString * s=[[libBundle resourcePath ] stringByAppendingPathComponent : filename];   
  7. NSLog ( @"%@" ,s);   
  8. return s;   
  9. }   
  10. return nil ;   
  11. }  

The getMyBundlePath function can obtain the absolute file path of a specific resource in the bundle yhyLibraryBundle, for example:

 
 
  1. /Users/kmyhy/Library/Application Support/iPhone Simulator/4.2/Applications/8213652F-A47E-456A-A7BB-4CD40892B66D/yhyLibTest.app/
  2. 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:

 
 
  1. [ 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:

 
 
  1. // Single-choice button group
  2. RadioGroup * rg = [[RadioGroup alloc] init];
  3. // 1st radio buttons
  4. CheckButton * cb = [[CheckButton alloc] initWithFrame: CGRectMake (20, 60,260, 32)];
  5. // Add the single-choice button to the button group
  6. [Rg add: cb];
  7. Cb. label. text = @"★";
  8. Cb. value = [[NSNumber alloc] initWithInt: 1];
  9. // Set the button as a single-choice button style
  10. Cb. style = CheckButtonStyleRadio;
  11. // Add View
  12. [Self. window addSubview: cb];
  13. [Cb release]; // after adding, it is automatically held and can be released
  14. // 2nd radio buttons
  15. Cb = [[CheckButton alloc] initWithFrame: CGRectMake (20,100,260, 32)];
  16. [Rg add: cb];
  17. Cb. label. text = @"★★";
  18. Cb. value = [[NSNumber alloc] initWithInt: 2];
  19. Cb. style = CheckButtonStyleRadio;
  20. [Self. window addSubview: cb];
  21. [Cb release];
  22. // 3rd radio buttons
  23. Cb = [[CheckButton alloc] initWithFrame: CGRectMake (20,140,260, 32)];
  24. [Rg add: cb];
  25. Cb. label. text = @"★★★";
  26. Cb. value = [[NSNumber alloc] initWithInt: 3];
  27. Cb. style = CheckButtonStyleRadio;
  28. [Self. window addSubview: cb];
  29. [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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.