码迷,mamicode.com
首页 > 其他好文 > 详细

第三方,解决模型无法在获取网络数据之后传值问题

时间:2015-07-30 19:03:29      阅读:98      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

导入这个第三方

   Model.h
 1 #import <Foundation/Foundation.h>
 2 @class Model;
 3 
 4 @protocol ModelDelegate <NSObject>
 5 
 6 -(void)didModel:(Model*)model receiveArray:(NSArray*)array;
 7 @end
 8 
 9 @interface Model : NSObject
10 
11 @property (nonatomic,copy) NSString *code;
12 @property (nonatomic,copy) NSString *juLogo;
13 @property (nonatomic,copy) NSString *juSlogo;
14 @property (nonatomic,copy) NSString *juBanner;
15 @property (nonatomic,copy) NSString *juBrand_id;
16 @property (nonatomic,copy) NSString *name;
17 @property (nonatomic,copy) NSString *juDiscount;
18 @property (nonatomic, weak) id<ModelDelegate> delegate;
19 
20 
21 - (void)getDataWithUrl:(NSString *)url;
22 @end
    Model.m
 1 #import "Model.h"
 2 #import "AFNetworking.h"
 3 
 4 @interface Model()
 5 @end
 6 
 7 @implementation Model
 8 
 9 - (void)getDataWithUrl:(NSString *)url {
10     
11     NSMutableArray *data_array = [NSMutableArray arrayWithCapacity:0];
12     
13     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
14     [manager POST:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject){
15         NSArray *array = responseObject[@"data"];
16         sleep(3);
17         for (NSDictionary *dic in array)
18         {
19             Model *dataModel = [[Model alloc]init];
20             [dataModel setValuesForKeysWithDictionary:dic];
21             [data_array addObject:dataModel];
22             
23         }
24         
25         if (self.delegate && [self.delegate respondsToSelector:@selector(didModel:receiveArray:)]) {
26             [self.delegate didModel:self receiveArray:data_array];
27         }
28         
29     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
30         NSLog(@"error---%@",error);
31     }];
32 }
33 
34 @end
    ViewController.m
 1 #import "ViewController.h"
 2 #import "Model.h"
 3 #import "AFNetworking.h"
 4 #import "MBProgressHUD.h"
 5 
 6 @interface ViewController () <ModelDelegate> {
 7     MBProgressHUD *_hud;
 8     Model *model;
 9 }
10 @end
11 
12 @implementation ViewController
13 
14 - (void)viewDidLoad
15 {
16     [super viewDidLoad];
17     
18     model = [[Model alloc] init];
19     model.delegate = self;
20     [model getDataWithUrl:@"http://zhekou.repai.com/jinrituangou/view/jupinpai_list.php"];    dispatch_queue_t queue = dispatch_queue_create("showhud", NULL);
21     dispatch_async(queue, ^{
22         dispatch_async(dispatch_get_main_queue(), ^{
23             [self showHUD:@"loading..."];
24         });
25     });
26 }
27 
28 -(void)showHUD:(NSString*)title {
29     if (_hud) {
30         [_hud hide:YES];
31         _hud = nil;
32     }
33     _hud = [MBProgressHUD showHUDAddedTo:[[[UIApplication sharedApplication] delegate] window] animated:YES];
34     _hud.labelText = title;
35     [_hud show:YES];
36 }
37 
38 -(void)closeHud {
39 
40 
41     [_hud hide:YES];
42 }
43 
44 -(void)didModel:(Model*)model receiveArray:(NSArray*)array {
45 //    NSLog(@"receive array: %@", array);
46     [self closeHud];
47     
48     [self logWithArray:array];
49     
50 }
51 
52 - (void)logWithArray:(NSArray *)array{
53     sleep(3);
54     NSLog(@"%@",array);
55 }
56 @end

 

第三方,解决模型无法在获取网络数据之后传值问题

标签:

原文地址:http://www.cnblogs.com/LiuLady12138/p/4684235.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!