iOS組件化 - 項目組件化

上一篇:iOS組件化 - 基礎html

筆記相關Git倉庫

前一篇主要記錄如何經過CocoaPod實現組件化。這裏則準備對Demo進行具體的組件化實現:建立一個登陸模塊的組件:DYDemoLogin666。登陸的邏輯、界面及用戶信息等都在這個組件。方便之後其餘功能模塊的開發。ios

一、與建立DYDemoTools同樣,建立DYDemoLogin666,完整的目錄以下


二、Example工程裏PodFile

use_frameworks!

platform :ios, '10.0'

source 'https://github.com/CocoaPods/Specs.git'  # 官方庫
source 'http://github.com/liyunxin/DYDemoSpecs.git' # 私有庫

target 'DYDemoLogin666' do
  pod 'DYDemoTools'
  pod 'DYDemoLogin666', :path => '../'
end
複製代碼

三、podspec文件

Pod::Spec.new do |s|
  s.name             = 'DYDemoLogin666'
  s.version          = '0.0.1'
  s.summary          = 'A short description of DYDemoLogin666.'

  s.description      = <<-DESC
  TODO: Add long description of the pod here.
  DESC

  s.homepage         = 'https://github.com/liyunxin/DYDemoLogin666'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'liyunxin' => '447674668@qq.com' }
  s.source           = { :git => 'https://github.com/liyunxin/DYDemoLogin666.git', :tag => s.version.to_s }

  s.platform   = :ios, "10.0"
  s.frameworks = 'UIKit'

  s.dependency "DYDemoTools"
  
  s.subspec '0_Base' do |sb|
    sb.source_files = "DYDemoLogin666/Base/**/*.{h,m}"
    sb.resource_bundles = {
      'DYDemoLogin666' => ['DYDemoLogin666/Base/*.xcassets']
    }
  end
  
  s.subspec '1_Tools' do |st|
    st.source_files = "DYDemoLogin666/Tools/**/*.{h,m}"
  end
  
  s.subspec '2_Data' do |st|
    st.source_files = "DYDemoLogin666/Data/**/*.{h,m}"
  end
  
  s.subspec '3_Controller' do |sv|
    sv.source_files = "DYDemoLogin666/Controller/**/*.{h,m}"
  end
  
end
複製代碼

四、DYDemoLogin666組件

具體實現請查看: DYDemoLogin666

4.一、DYDemoLoginHeadergit

這是在組件內部使用的頭文件,導入了依賴的庫跟全局使用的分類等。它只會出如今組件的.m文件裏,毫不能暴露在.h文件中。

#ifndef DYDemoLoginHeader_h
#define DYDemoLoginHeader_h

#import "DYDemoTools/DYDemoToolsHeader.h"
#import "DYDemoTools+DLCategory.h"

#endif /* DYDemoLoginHeader_h */複製代碼

4.二、DYDemoLoginDatagithub

這是存放用戶數據的單例。單例的好處是能夠在其餘組件中使用。

4.三、DYDemoLoginbash

這是組件對外的入口,其餘項目使用DYDemoLogin666組件時只容許DYDemoLogin做爲入口, 這種作法是爲了下降耦合。作法不惟一,這裏也能夠採用Casa Taloyum的組件化解藕方案: CTMediator

五、組件導入主工程

模塊組件與基礎組件差異在於:模塊組件不須要分版本。 因此這裏選擇另外一種導入方案:不提交至私有Repo而直接導入。

5.一、DYDemo666的Podfile修改以下,並對工程進行pod update組件化

use_frameworks!

platform :ios, '10.0'

source 'https://github.com/CocoaPods/Specs.git'  # 官方庫
source 'http://github.com/liyunxin/DYDemoSpecs.git' # 私有庫

target 'DYDemo666' do
  pod 'DYDemoTools'
  pod 'DYDemoLogin666', :git => 'https://github.com/liyunxin/DYDemoLogin666.git' 
end複製代碼

5.二、添加PCH文件,並導入組件post

#ifndef DYDemo666_pch
#define DYDemo666_pch

#import "DYDemoTools/DYDemoToolsHeader.h"
#import "DYDemoLogin666/DYDemoLogin.h"

#endif /* DYDemo666_pch */
複製代碼

5.三、接下來就能夠在主項目裏面使用DYDemo666組件的功能了,效果以下:ui


六、CTMediator的具體運用

CTMediator實現解藕的原理請查看 原文

6.一、在DYDemoTools中添加CTMediator文件,並從新打Tag提交到Repourl

6.二、在DYDemoLogin666添加我的信息界面spa

6.三、實現CTMediator相關邏輯文件

添加CTMediator的分類:CTMediator+DYDemoLogin

此分類名字可隨意

#import "DYDemoTools/CTMediator.h"

@interface CTMediator (DYDemoLogin)

- (UIViewController *)ingoVC;

@end
複製代碼

#import "CTMediator+DYDemoLogin.h"

@implementation CTMediator (DYDemoLogin)

- (UIViewController *)ingoVC {
    return [self performTarget:@"DYDemoLogin"
                        action:@"InfoVC"
                        params:@{}
             shouldCacheTarget:NO];
}

@end複製代碼

添加Target_DYDemoLogin類:

該類名稱格式固定,成員方法的名稱格式也固定

#import <Foundation/Foundation.h>

@interface Target_DYDemoLogin : NSObject

- (UIViewController *)Action_InfoVC:(NSDictionary *)param;
- (UIViewController *)Action_AboutVC:(NSDictionary *)param;

@end
複製代碼

#import "Target_DYDemoLogin.h"
#import "DYInfoController.h"

@implementation Target_DYDemoLogin

- (UIViewController *)Action_InfoVC:(NSDictionary *)param {
    return [[DYInfoController alloc] init];
}

- (UIViewController *)Action_AboutVC:(NSDictionary *)param {
    UIViewController *vc = [[UIViewController alloc] init];
    vc.view.backgroundColor = [UIColor whiteColor];
    vc.navigationItem.title = [NSString stringWithFormat:@"關於咱們-%@", param[@"name"]];
    return vc;
}

@end
複製代碼

七、主項目中經過CTMediator

在進行使用以前給主項目配置一個Scheme:dydemo666

//可經過如下方法進行頁面跳轉  
UIViewController *vc1 = [[CTMediator sharedInstance] ingoVC];
[self.navigationController pushViewController:vc1 animated:YES];

NSURL *url2 = [NSURL URLWithString:@"dydemo666://DYDemoLogin/InfoVC]; UIViewController *vc2 = [[CTMediator sharedInstance] performActionWithUrl:url2 completion:nil]; [self.navigationController pushViewController:vc2 animated:YES]; NSURL *url3 = [NSURL URLWithString:@"dydemo666://DYDemoLogin/AboutVC?name=heiheihei"]; UIViewController *vc3 = [[CTMediator sharedInstance] performActionWithUrl:url3 completion:nil]; [self.navigationController pushViewController:vc3 animated:YES];複製代碼

具體請查看DYDemo666

相關文章
相關標籤/搜索