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


    导入这个第三方

       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
  • 相关阅读:
    格式布局
    tp框架之文件上传
    tp框架之验证码
    tp框架之自动验证表单
    tp框架之留言板练习
    tp框架之session
    tp框架之登录验证
    tp框架之函数调用
    tp框架之分页与第三方类的应用
    tp框架之AJAX
  • 原文地址:https://www.cnblogs.com/LiuLady12138/p/4684235.html
Copyright © 2020-2023  润新知