ReactiveCocoa简单实战 (一)
ReactiveCocoa简单实战我厂广招各路大神加入:job.koudaitong.com 前戏今天从杭州回家错过了高铁,又改成了客车。本来非常懊恼的心情,看到文章被SF博客转发了,一下子就好了起来。 最近闲着也是为了下一个与TX的小伙伴合作的项目做准备,做了一个简单的APP。主要的功能就是设定一个目的地,在你快要到达目的地的时候给你提醒。对于我这种坐动车常做过站的人来说,恩,时候是拯救自己一把了。 欲露还羞项目的结构很简单,如下图所示:
项目使用了MVVM的框架结构来替代MVC框架。storyboard中的内容如下:
第一个界面为保存的路线,选择之后直接进入出发页面,新增路线之后点击出发同样进入出发页面。在出发页面中点击changeBtn( 正戏开始首先,将高德地图的SDK配置好之后,来看看第一个页面--ChooseViewController的内容: #import "ChooseViewController.h" #import "ChooseViewModel.h" #import <AMapSearchKit/AMapSearchObj.h> #import "OnRoadViewController.h" #import <PromiseKit/PromiseKit.h> @interface ChooseViewController () @property (nonatomic,strong) ChooseViewModel *viewModel; @property (nonatomic,strong) NSDictionary *targetInfoDic; @end @implementation ChooseViewController - (void)viewDidLoad { [super viewDidLoad]; _viewModel = [ChooseViewModel new]; self.tableView.delegate = _viewModel; self.tableView.dataSource = _viewModel; [_viewModel.cellSelectedSignal subscribeNext:^(id x) { if (x) { //tiaozhuan _targetInfoDic = x; [self performSegueWithIdentifier:@"SelectToGo" sender:self]; } }]; [_viewModel.loadRoutesFromCache subscribeNext:^(id x) { dispatch_async(dispatch_get_main_queue(),^{ [self.tableView reloadData]; }); } error:^(NSError *error) { NSLog(@"%@",error.description); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([segue.identifier isEqualToString:@"SelectToGo"]) { CGPoint location = [_targetInfoDic[@"target"] CGPointValue]; NSDictionary *info = _targetInfoDic[@"target_info"]; AMapPOI *targetPoi = [[AMapPOI alloc] init]; AMapGeoPoint *mgp = [AMapGeoPoint locationWithLatitude:location.x longitude:location.y]; targetPoi.location = mgp; targetPoi.name = info[@"name"]; targetPoi.address = info[@"address"]; targetPoi.type = info[@"type"]; OnRoadViewController *onRoadVC = (OnRoadViewController *)[segue destinationViewController]; onRoadVC.targetPoi = targetPoi; } } @end 很简单的一段代码。在匿名类别中添加了两个对象,一个就是ViewModel,另一个用来储存目的地的经纬座标的字典。 在 那么我们就来看看这两个事件到底是怎么起作用的吧。在 -(RACSignal *)loadRoutesFromCache; @property (nonatomic,strong) RACSubject *cellSelectedSignal; 在对象被new的时候,我们实例化了 +(id)new{ ChooseViewModel *model = [super new]; model.cellSelectedSignal = [RACSubject subject]; return model; } ( -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ [self.cellSelectedSignal sendNext:self.waysDic[self.routesArr[indexPath.row]]]; } 我们使用了sendNext:方法将 接下来看看那个从文件中读取内容的信号吧( -(RACSignal *)loadRoutesFromCache{ RACSubject *subject = [RACSubject subject]; dispatch_async(dispatch_get_global_queue(0,0),^{ NSData *data = [NSData dataWithContentsOfFile:kWaysSavePath]; NSMutableDictionary *waysDic = [NSKeyedUnarchiver unarchiveObjectWithData:data]; if(waysDic!=nil && [waysDic allKeys].count>0){ self.waysDic = waysDic; self.routesArr = [waysDic allKeys]; [subject sendNext:_routesArr]; [subject sendCompleted]; }else{ [subject sendError:[NSError errorWithDomain:@"rb.loadroutes" code:0 userInfo:@{@"error": @"未能加载"}]]; }; }); return subject; } 这个信号被表示成了一个方法,这个方法返回了一个Subject对象。在异步执行完成后,向这个Subject对象发送next与completed信号,如果内容不存在则发送error信号。VC中( 这么快就没了恩……男人是不是不能这么快……好吧,这次先写到这儿,好困啊,明天项目正式动工,先早点休息了。 下次为大家继续新建路线中的内容。( 代码写的到不到的还请多多包含,最好能帮忙指出错误!小弟谢过了。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |