Componentization of iOS III

Get into the habit of writing together! This is the 16th day of my participation in the "Nuggets Daily New Plan·April Update Challenge", click to view the event details .

Loading of resource files

Image resources

In daily development, using image resource files, all methods will UIImagebe imageNamedused

self.imageView.image = [UIImage imageNamed:@"share_wechat"];
复制代码
  • But the picture used is in the component project, which cannot be accessed in this way

The image resource storage location in the component, in the 组件/Assetsdirectory

image-57.pngThe file that configures the component Podand writes to the resourceBundle

image-58.pngIn the test project Exampledirectory, executepod install

In the test project, Bundleaccess the image resources in the component by specifying

NSString *bundlePath = [[NSBundle bundleForClass:[self class]].resourcePath stringByAppendingPathComponent:@"/LGModuleTest.bundle"];
NSBundle *resoure_bundle = [NSBundle bundleWithPath:bundlePath];

self.imageView.image = [UIImage imageNamed:@"share_wechat" inBundle:resoure_bundle compatibleWithTraitCollection:nil];
复制代码
jsondocument

Encapsulate HomeViewController in the component and read the jsonfile in the component

jsonThe configuration path of the file, in the 组件/Assetsdirectory

image-59.pngThe file that configures the component Podand writes to the resourceBundle

image-60.pngIn the test project Exampledirectory, executepod install

read mode, specifyLGHomeModule.bundle

NSString *bundlePath = [[NSBundle bundleForClass:[self class]].resourcePath stringByAppendingPathComponent:@"/LGHomeModule.bundle"];
NSString *path = [[NSBundle bundleWithPath:bundlePath] pathForResource:[NSString stringWithFormat:@"Home_TableView_Response_%@", channelId] ofType:@"json"];

NSData *data = [NSData dataWithContentsOfFile:path];
复制代码
xibdocument

There is also a resource file, which is very similar to the picture, which is the xibfile that is often used in our development.

Access xibfiles within components

image-61.pngread mode, specifyBundle

for (NSString *className in HomeTableViewCellIdentifiers.allValues) {
    
    NSString *bundlePath = [NSBundle bundleForClass:[self class]].resourcePath;

    [self.tableView registerNib:[UINib nibWithNibName:className bundle:[NSBundle bundleWithPath:bundlePath]] forCellReuseIdentifier:className];
}
复制代码

Communication decoupling

Modules at the same level communicate with each other, resulting in complicated communication codes. You have me I have you. The modification of a single module is likely to involve errors in other modules, which is not conducive to the maintenance of the project

image-62.pngAt this time, the communication between modules should be sinking, and a common lower-level component should be extracted, so that the modules are decoupled and the codes are independent of each other.

image-63.pngFor the above communication decoupling requirements, mainstream solutions can be divided into three types:

  • URLrouting
  • target-action
  • protocol

We describe these options in detail in subsequent articles.

Guess you like

Origin juejin.im/post/7087189214382538766