搭建App框架(MVVM+RAC+路由)
如果喜歡理逊,請給一個(gè)小星星。O(∩_∩)O謝謝准给!
效果
項(xiàng)目的介紹
項(xiàng)目使用了ReactiveCocoa框架王污,實(shí)現(xiàn)了MVVM架構(gòu),使用MVVM架構(gòu)主要目的是分離視圖(View)和模型(Model)蔬顾。
MVVM四大優(yōu)點(diǎn):
- 低耦合宴偿。視圖(View)可以獨(dú)立于Model變化和修改,一個(gè)ViewModel可以綁定到不同的"View"上诀豁,當(dāng)View變化的時(shí)候Model可以不變酪我,當(dāng)Model變化的時(shí)候View也可以不變。
- 可重用性且叁。你可以把一些視圖邏輯放在一個(gè)ViewModel里面都哭,讓很多view重用這段視圖邏輯。
- 獨(dú)立開發(fā)逞带。開發(fā)人員可以專注于業(yè)務(wù)邏輯和數(shù)據(jù)的開發(fā)(ViewModel)欺矫,設(shè)計(jì)人員可以專注于頁面設(shè)計(jì),使用Expression
Blend可以很容易設(shè)計(jì)界面并生成xaml代碼展氓。 - 可測試穆趴。界面素來是比較難于測試的,而現(xiàn)在測試可以針對ViewModel來寫
項(xiàng)目的搭建
1.搭建項(xiàng)目主框架
2.CocoaPods集成的第三方框架
- AFNetworking 網(wǎng)絡(luò)請求
- DateTools 日期的相對處理
- JLRoutes 路由跳轉(zhuǎn)
- JPFPSStatus 顯示屏幕的幀數(shù)
- MBProgressHUD 蒙版
- MJExtension 字典轉(zhuǎn)模型框架
- MJRefresh 界面刷新
- MYLayout 浮動(dòng)布局
- ReactiveCocoa 函數(shù)響應(yīng)式編程
- SDAutoLayout 簡單的布局
- SDCycleScrollVIew 無限循環(huán)輪播圖
- SDWebImage 加載圖片
3.初步搭建界面
1.初始化項(xiàng)目
- (void)registerRoutes{
HQTabBarController *rootVC = [HQTabBarController tabBarControllerWithAddChildVCsBlock:^(WZXTabBarController *tabBarC) {
[tabBarC addChildVC:[HQHomeViewController new] title:@"主頁" normalImageName:@"tabar_zhuye2.png" selectedImageName:@"tabar_zhuye.png" isRequiredNavController:YES];
[tabBarC addChildVC:[HQCircleFriendsViewController new] title:@"主頁2" normalImageName:@"tabar_tuijian2.png" selectedImageName:@"tabar_tuijiani.png" isRequiredNavController:YES];
[tabBarC addChildVC:[HQHomeViewController new] title:@"中間按鈕" normalImageName:@"tabar_suishoupai2.png" selectedImageName:@"tabar_suishoupai.png" isRequiredNavController:YES];
[tabBarC addChildVC:[HQFindViewController new] title:@"朋友" normalImageName:@"tabar_linxin2.png" selectedImageName:@"tabar_linxin.png" isRequiredNavController:YES];
[tabBarC addChildVC:[HQAccountViewController new] title:@"我的" normalImageName:@"tabar_geren2.png" selectedImageName:@"tabar_geren.png" isRequiredNavController:YES];
}];
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = rootVC;
[self.window makeKeyAndVisible];
// navigation Push規(guī)則
[[JLRoutes globalRoutes] addRoute:@"/NaviPush/:controller" handler:^BOOL(NSDictionary<NSString *,NSString *> * _Nonnull parameters) {
NSLog(@"parameters==%@",parameters);
UIViewController *v = [[NSClassFromString(parameters[@"controller"]) alloc] init];
[self paramToVc:v param:parameters];
[[self currentViewController] pushViewController:v animated:YES];
return YES;
}];
}
2.路由跳轉(zhuǎn)
-(void)paramToVc:(UIViewController *) v param:(NSDictionary<NSString *,NSString *> *)parameters{
// runtime將參數(shù)傳遞至需要跳轉(zhuǎn)的控制器
unsigned int outCount = 0;
objc_property_t * properties = class_copyPropertyList(v.class , &outCount);
for (int i = 0; i < outCount; i++) {
objc_property_t property = properties[i];
NSString *key = [NSString stringWithUTF8String:property_getName(property)];
NSString *param = parameters[key];
if (param != nil) {
[v setValue:param forKey:key];
}
}
}
/**
* 獲取當(dāng)前控制器
*/
-(UINavigationController *)currentViewController{
HQTabBarController *HQTabBar = (HQTabBarController *)self.window.rootViewController;
return HQTabBar.selectedViewController;
}
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
NSLog(@"從哪個(gè)app跳轉(zhuǎn)而來 Bundle ID: %@", options[UIApplicationOpenURLOptionsSourceApplicationKey]);
NSLog(@"URL scheme:%@", [url scheme]);
#pragma mark - JLRoutes(默認(rèn)的Scheme)
return [[JLRoutes globalRoutes] routeURL:url];
}
- (void)urlClick{
// 發(fā)送請求
RACSignal *signal = [[[HQHomeViewModel alloc]init].requestCommand execute:@{@"code":@"Home"}];
[signal subscribeNext:^(id x) {
NSLog(@"%@",x);
NSString *customURL = @"HQMVVMRACRouter://NaviPush/TwoViewController?name=home&userId=10000&age=18&adbc=29";
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:customURL] options:@{UIApplicationOpenURLOptionsSourceApplicationKey : @YES} completionHandler:nil];
}];
}
希望各位大神指出不足的地方遇汞,大家共同進(jìn)步未妹!謝謝。