看了大神示例工程LCLive(面向协议组件化思路),我来总结几个步骤:
LCMediator 作为一个单独的子工程 ,定义各模块协议。只负责定义方法,不负责实现,模块解耦。
1、创建各个模块的协议 LCChatModuleProtocol.h
/**私信模块协议*/
@protocol LCChatModule <NSObject>
/***/
- (UIViewController *)sessionListViewController;
/**读取未读数*/
- (NSUInteger)unreadCount;
/**跳转到单聊聊天详情*/
- (void)pushUserChatViewControllerWithUserId:(NSString *)userId fromViewController:(UIViewController *)viewController;
2、业务工程LCChat中,定义Module类,遵循协议,控制器的实例化与push跳转动作都写在Module中
#import "LCChatModuleProtocol.h"
@interface LCChatModule : NSObject<LCChatModule>
@end
@implementation LCChatModule
- (UIViewController *)sessionListViewController {
LCSessionListViewController *vc = [[LCSessionListViewController alloc]init];
returnvc;
}
- (NSUInteger)unreadCount {
return3;
}
- (void)pushUserChatViewControllerWithUserId:(NSString *)userId fromViewController:(UIViewController *)viewController {
LCUserChatViewController *userChatVC = [[LCUserChatViewController alloc]init];
[viewController.navigationController pushViewController:userChatVC animated:YES];
}
@end
3、LCLaunch 工程中,配置控制器初始化的代码
在appDelegate类中执行
- (BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[LCGetModuleInstance(LCLaunchModule)handleDidFinishLaunchingApplication:applicationoptions:launchOptionswindow:self.window];
return YES;
}
LCLaunchModule类中封装控制器的创建
- (void)handleDidFinishLaunchingApplication:(UIApplication*)applicationoptions:(NSDictionary*)launchOptionswindow:(UIWindow*)window{
window.backgroundColor = [UIColor whiteColor];
NSArray*titles =@[@"消息",@"动态",@"我的"];
NSArray *imageTags = @[@"message",@"moment",@"me"];
NSMutableArray*viewControllers = [NSMutableArrayarray];
for(NSIntegeri =0; i < titles.count; i ++) {
UIViewController*vc =nil;
switch(i) {
case0:{
vc = [LCGetModuleInstance(LCChatModule)sessionListViewController];
}
break;
case1:{
vc = [LCGetModuleInstance(LCMomentModule)momentListViewController];
}
break;
case2:{
vc = [LCGetModuleInstance(LCMeModule)meViewController];
}
break;
default:
break;
}
// NSLog(@"vc = %@",vc);
UITabBarItem *tabBarItem = [[UITabBarItem alloc]initWithTitle:titles[i] image:[UIImage imageNamed:[NSString stringWithFormat:@"lc_root_tab_%@_normal",imageTags[i]]] selectedImage:[UIImage imageNamed:[NSString stringWithFormat:@"lc_root_tab_%@_pressed",imageTags[i]]]];
vc.tabBarItem= tabBarItem;
UINavigationController *nc = [[UINavigationController alloc]initWithRootViewController:vc];
// LCNavigationController *nc = [[LCNavigationController alloc]initWithRootViewController:vc];
[viewControllersaddObject:nc];
}
UITabBarController *tbc = [[UITabBarController alloc]init];
tbc.viewControllers= viewControllers;
window.rootViewController= tbc;
[windowmakeKeyAndVisible];
}
网友评论