BeeFramework 系列三 MVC篇上

  这两天死了不少人,南北呼应。厦门的兄弟们别挤brt了,公交有风险,挤车需谨慎! 


    继续介绍Bee的mvc,框架中已经为MVC做了一定程度的封装,我们查看Bee的源码会发现对应着BeeController、BeeModel以及各种常用的View和ViewController,另外ViewLayout添加了View界面布局的灵活性,从v0.3.0版本开始更是加入了基于XML的UI模板模块。 

    

    还是先上个例子比较直观些。 

     

    这个例子,通过访问国家气象局天气预报JSON数据接口,http://www.weather.com.cn/data/sk/101010100.html,返回天气数据。 

    看下目录结构 

     

    把MVC分离出来放到不同的目录下。 

    Model层代码: 

   

Java代码  

  1. //  
  2. //  WeatherModel.h  
  3. //  BeeFrameWorkTest  
  4. //  
  5. //  Created by he songhang on 13-6-8.  
  6. //  Copyright (c) 2013年 he songhang. All rights reserved.  
  7. //  
  8.   
  9. #import <BeeFramework/Bee.h>  
  10.   
  11. @interface Weather : BeeActiveRecord  
  12.   
  13. @property(nonatomic,strong) NSString *city;//城市  
  14. @property(nonatomic,strong) NSString *temp;//气温  
  15. @property(nonatomic,strong) NSString *WD;//风向  
  16. @property(nonatomic,strong) NSString *WS;//风力  
  17. @property(nonatomic,strong) NSString *time;//发布时间  
  18.   
  19. @end  
  20.   
  21. @interface WeatherModel : BeeModel  
  22.   
  23. @property(nonatomic,strong) Weather *weather;  
  24.   
  25. -(void)loadOnline;//查询天气  
  26.   
  27. -(void)loadCache;//读取历史记录  
  28.   
  29. @end  



Java代码  

  1. //  
  2. //  WeatherModel.m  
  3. //  BeeFrameWorkTest  
  4. //  
  5. //  Created by he songhang on 13-6-8.  
  6. //  Copyright (c) 2013年 he songhang. All rights reserved.  
  7. //  
  8.   
  9. #import "WeatherModel.h"  
  10. #import "WeatherController.h"  
  11.   
  12. @implementation Weather  
  13.   
  14. +(void)mapRelation{  
  15.     [super mapPropertyAsKey:@"city"];  
  16.     [super mapRelation];  
  17. }  
  18.   
  19. @end  
  20.   
  21. @interface WeatherModel (private)  
  22.   
  23. - (void)saveCache;  
  24. - (void)asyncSaveCache;  
  25.   
  26. @end  
  27.   
  28. @implementation WeatherModel  
  29.   
  30. -(void)loadOnline{  
  31.     [self sendMessage:WeatherController.LOAD_WEATHER];  
  32. }  
  33.   
  34. -(void)loadCache{  
  35.     int count = Weather.DB.WHERE(@"city",@"北京").COUNT().resultCount;  
  36.     if (count>0) {  
  37.         self.weather = [Weather.DB.WHERE(@"city",@"北京").GET_RECORDS() lastObject];  
  38.     }  
  39. }  
  40.   
  41. - (void)saveCache  
  42. {  
  43.     [NSObject cancelPreviousPerformRequestsWithTarget:self];  
  44.     [self performSelector:@selector(asyncSaveCache) withObject:nil afterDelay:0.1f];  
  45. }  
  46.   
  47.   
  48. - (void)asyncSaveCache  
  49. {  
  50.     _weather.SAVE();  
  51. }  
  52.   
  53. - (void)handleMessage:(BeeMessage *)msg  
  54. {  
  55.     [super handleMessage:msg];  
  56. }  
  57.   
  58. - (void)handleWeatherController:(BeeMessage *)msg  
  59. {  
  60.     if ( [msg is:WeatherController.LOAD_WEATHER] )  
  61.     {  
  62.         if ( msg.succeed )  
  63.         {  
  64.             NSDictionary *dict = [msg.output dictAtPath:@"result"];  
  65.             if (dict) {  
  66.                 self.weather = (Weather *)[dict objectForClass:[Weather class]];  
  67.                 [self saveCache];  
  68.             }  
  69.               
  70.         }  
  71.     }  
  72.     [super handleMessage:msg];  
  73. }  
  74.   
  75. @end  


     在.h中定义了一个Weather的实体,与天气数据对应,用到了BeeActiveRecord,这是Bee中的ORM框架,实现数据库与BeeActiveRecord的序列和反序列化。通常我们在做离线缓存时采用CoreData,bee采用的是DB的方式。感谢老郭为我们封装了原来的SQL语法,免去了封装枯燥的SQL语句。查看BeeActiveRecord的父类BeeDatabase,可以看到一些开源软件上用的很欢的以Block为参数的写法。 

     在.m中通过

Java代码  

  1. [self sendMessage:WeatherController.LOAD_WEATHER];  

向Weathercontroller发送了一个LOAD_WEATHER的请求,在handleWeatherController中响应BeeMessage的不同状态,并完成离线数据的保存工作

Java代码  

  1. [self saveCache];  



      再上Controller的实现 

Java代码  

  1. //  
  2. //  WeatherController.h  
  3. //  BeeFrameWorkTest  
  4. //  
  5. //  Created by he songhang on 13-6-8.  
  6. //  Copyright (c) 2013年 he songhang. All rights reserved.  
  7. //  
  8.   
  9. #import "Bee_Controller.h"  
  10.   
  11. @interface WeatherController : BeeController  
  12.   
  13. AS_MESSAGE(LOAD_WEATHER)  
  14.   
  15. @end  



Java代码  

  1. //  
  2. //  WeatherController.m  
  3. //  BeeFrameWorkTest  
  4. //  
  5. //  Created by he songhang on 13-6-8.  
  6. //  Copyright (c) 2013年 he songhang. All rights reserved.  
  7. //  
  8.   
  9. #import "WeatherController.h"  
  10. #import "JSONKit.h"  
  11. #import <BeeFramework/Bee.h>  
  12.   
  13. @implementation WeatherController  
  14.   
  15. DEF_MESSAGE(LOAD_WEATHER)  
  16.   
  17. -(void)LOAD_WEATHER:(BeeMessage *)msg{  
  18.     if ( msg.sending )  
  19.     {  
  20.         NSString * callURL = @"http://www.weather.com.cn/data/sk/101010100.html" ;  
  21.         msg.HTTP_GET( callURL );  
  22.     }  
  23.     else if ( msg.progressed )  
  24.     {  
  25.           
  26.     }  
  27.     else if ( msg.succeed )  
  28.     {  
  29.         NSDictionary * jsonData = [msg.response objectFromJSONData];  
  30.         if ( nil == jsonData )  
  31.         {  
  32.             [msg setLastError];  
  33.             return;  
  34.         }  
  35.           
  36.         NSDictionary *weatherinfo = (NSDictionary *)[jsonData objectAtPath:@"weatherinfo"];  
  37.                   
  38.         [msg output:@"result", weatherinfo, nil];  
  39.     }  
  40.     else if ( msg.failed )  
  41.     {  
  42.           
  43.     }  
  44.     else if ( msg.cancelled )  
  45.     {  
  46.     }  
  47. }  
  48.   
  49. @end  


     WeatherController完成与天气预报服务器交互的工作,和UISignle类似,在.h中通过AS_MESSAGE(LOAD_WEATHER)定义了一个LOAD_WEATHER的方法声明,在.m中用DEF_MESSAGE(LOAD_WEATHER)对应,并完成LOAD_WEATHER的方法实现。在bee中网络层使用的是ASIHTTPRequest框架,至于为什么不用AFNetworking,这实际上是个人习惯的问题,虽然ASIHTTPRequest已经N久没更新过了,不过也够用了。BeeMessage中对ASIHTTPRequest做了很多工作,通过BeeRequestQueue,对post和get过程中状态的分离。 


    再来看view层,其实在这个例子中做真正做逻辑的是ViewController,注意它的父类为BeeUIBoard,正是这个类做了UIMessage的消息转发,还实现了ViewController生命周期中的UISignal化。 

Java代码  

  1. //  
  2. //  ViewController.h  
  3. //  BeeFrameWorkTest  
  4. //  
  5. //  Created by he songhang on 13-6-3.  
  6. //  Copyright (c) 2013年 he songhang. All rights reserved.  
  7. //  
  8.   
  9. #import <UIKit/UIKit.h>  
  10. #import <BeeFramework/Bee.h>  
  11. #import "WeatherModel.h"  
  12.   
  13. @interface ViewController : BeeUIBoard{  
  14.     __strong WeatherModel *_weatherModel;  
  15.     __weak IBOutlet UITextView *textView;  
  16. }  
  17.   
  18. @end  



Java代码  

  1. //  
  2. //  ViewController.m  
  3. //  BeeFrameWorkTest  
  4. //  
  5. //  Created by he songhang on 13-6-3.  
  6. //  Copyright (c) 2013年 he songhang. All rights reserved.  
  7. //  
  8.   
  9. #import "ViewController.h"  
  10. #import "WeatherController.h"  
  11.   
  12. @implementation ViewController  
  13.   
  14. -(void)load{  
  15.     _weatherModel = [[WeatherModel alloc]init];  
  16.     [_weatherModel addObserver:self];  
  17. }  
  18.   
  19. - (void)viewDidLoad  
  20. {  
  21.     [super viewDidLoad];  
  22.     // Do any additional setup after loading the view, typically from a nib.  
  23. }  
  24.   
  25. - (void)didReceiveMemoryWarning  
  26. {  
  27.     [super didReceiveMemoryWarning];  
  28.     // Dispose of any resources that can be recreated.  
  29. }  
  30.   
  31. - (void)handleUISignal:(BeeUISignal *)signal  
  32. {  
  33.     [super handleUISignal:signal];  
  34. }  
  35.   
  36. - (void)handleUISignal_BeeUIBoard:(BeeUISignal *)signal  
  37. {  
  38.     [super handleUISignal:signal];  
  39.       
  40.     if ( [signal is:BeeUIBoard.CREATE_VIEWS] )  
  41.     {  
  42.         [self showNavigationBarAnimated:YES];  
  43.         [self showBarButton:UINavigationBar.BARBUTTON_RIGHT system:UIBarButtonSystemItemRefresh];  
  44.         self.title = @"天气预报";  
  45.     }  
  46.     else if ( [signal is:BeeUIBoard.DELETE_VIEWS] )  
  47.     {  
  48.     }  
  49.     else if ( [signal is:BeeUIBoard.LOAD_DATAS] )  
  50.     {  
  51.         [_weatherModel loadCache];  
  52.         [self refushTextView];  
  53.     }  
  54.     else if ( [signal is:BeeUIBoard.WILL_APPEAR] )  
  55.     {  
  56.           
  57.     }  
  58.     else if ( [signal is:BeeUIBoard.DID_DISAPPEAR] )  
  59.     {  
  60.     }  
  61. }  
  62.   
  63. - (void)handleUISignal_UINavigationBar:(BeeUISignal *)signal  
  64. {  
  65.     if ( [signal is:UINavigationBar.BACK_BUTTON_TOUCHED] )  
  66.     {  
  67.     }  
  68.     else if ( [signal is:UINavigationBar.DONE_BUTTON_TOUCHED] )  
  69.     {  
  70.         [self loadOnline];  
  71.     }  
  72. }  
  73.   
  74. -(void)loadOnline{  
  75.     [_weatherModel loadOnline];  
  76. }  
  77.   
  78. -(void)refushTextView{  
  79.     if (_weatherModel.weather) {  
  80.         Weather *weather = _weatherModel.weather;  
  81.         NSString *weatherInfo = [NSString stringWithFormat:@"城市:%@\n气温:%@\n风向:%@\n风力%@\n发布时间:%@,",weather.city,weather.temp,weather.WD,weather.WS,weather.time];  
  82.         textView.text = weatherInfo;  
  83.     }  
  84. }  
  85.   
  86. - (void)handleMessage:(BeeMessage *)msg  
  87. {  
  88.     [super handleMessage:msg];  
  89. }  
  90.   
  91. - (void)handleWeatherController:(BeeMessage *)msg  
  92. {  
  93.     [super handleMessage:msg];  
  94.       
  95.     if ( [msg is:WeatherController.LOAD_WEATHER] )  
  96.     {  
  97.         if ( msg.sending )  
  98.         {  
  99.             BeeUIActivityIndicatorView * indicator = [BeeUIActivityIndicatorView spawn];  
  100.             [self showBarButton:UINavigationBar.BARBUTTON_RIGHT custom:indicator];  
  101.             [indicator startAnimating];  
  102.         }  
  103.         else  
  104.         {  
  105.             [self showBarButton:UINavigationBar.BARBUTTON_RIGHT system:UIBarButtonSystemItemRefresh];  
  106.         }  
  107.   
  108.           
  109.         if ( msg.succeed )  
  110.         {  
  111.             [self refushTextView];  
  112.         }  
  113.     }  
  114. }  
  115.   
  116. @end  



 


     这里应注意ViewController.m中的代码:

Java代码  

  1. -(void)load{  
  2.     _weatherModel = [[WeatherModel alloc]init];  
  3.     [_weatherModel addObserver:self];//注意点!!!  
  4. }  

,结合WeatherModel.m中的

Java代码  

  1. - (void)handleWeatherController:(BeeMessage *)msg  
  2. {  
  3.     if ( [msg is:WeatherController.LOAD_WEATHER] )  
  4.     {  
  5.         if ( msg.succeed )  
  6.         {  
  7.             NSDictionary *dict = [msg.output dictAtPath:@"result"];  
  8.             if (dict) {  
  9.                 self.weather = (Weather *)[dict objectForClass:[Weather class]];  
  10.                 [self saveCache];  
  11.             }  
  12.               
  13.         }  
  14.     }  
  15.     [super handleMessage:msg];//注意点  
  16. }  


     如果把

Java代码  

  1. [super handleMessage:msg];  

去掉,那么ViewController中的handleMessage和handleWeatherController将无法响应。 

     

     本篇通过天气预报介绍了Bee中的MVC的实现场景,当然你也可以根据自己的需要选择适用的模块,你可以在Model中直接实现网络层交互,也可以去掉model层,直接在UIBoard中调用BeeController,怎么选择还是看个人习惯。 

     

     以上代码下载:https://github.com/ilikeido/BeeFrameworkTest/tree/master/lesson4

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值