数据从块传递到视图控制器

时间:2016-10-17 07:13:05

标签: ios objective-c

我已经使用以下代码从服务器获取数据。现在我想在具有表视图单元的其他视图控制器上反映相同的内容。如何将此单例类中的数据传递给具有表视图单元格的类。请帮助目标c中的一些代码。提前谢谢!

-(NSDictionary *)getResponseFromSearchByRoutewithUrl:(NSString *)url {
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [urlRequest setHTTPMethod:@"GET"];
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSDictionary *responseDictionary;
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

    NSURLSessionDataTask *task = [session dataTaskWithRequest: urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        //check if we encountered an error
        if(error != nil){
            NSLog(@"%@", [error localizedDescription]);
        }else{
            //get and check the HTTP status code
            NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];
            if (HTTPStatusCode != 200) {
                NSLog(@"HTTP status code = %ld", (long)HTTPStatusCode);
            }

            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                if(data != nil){
                    NSError *parseError = nil;
                    NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                    NSLog(@"The response is - %@",responseDictionary);

                }
            }];
        }
    }];


    [task resume];
    return responseDictionary;
}

1 个答案:

答案 0 :(得分:0)

typedef void (^HttpCompletionBlock)     (NSString *token, NSError *error);

-(NSDictionary *)getResponseFromSearchByRoutewithUrl:(NSString *)url  :(HttpCompletionBlock)completionHandler {
    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [urlRequest setHTTPMethod:@"GET"];
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSDictionary *responseDictionary;
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

    NSURLSessionDataTask *task = [session dataTaskWithRequest: urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    //check if we encountered an error
    if(error != nil){
        NSLog(@"%@", [error localizedDescription]);
    }else{
        //get and check the HTTP status code
        NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];
        if (HTTPStatusCode != 200) {
            NSLog(@"HTTP status code = %ld", (long)HTTPStatusCode);
        }



        [task resume];
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            if(data != nil){
                NSError *parseError = nil;
                NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
                NSLog(@"The response is - %@",responseDictionary);
                return completionHandler(responseDictionary,nil);

            }
            else {
                 return completionHandler(nil,error);

            }
        }];
    }
}];

}

WS调用函数

- (IBAction)bankLoginPressed:(id)sender
{
    [HTTPClass getResponseFromSearchByRoutewithUrl:@"URL" :^(NSDictionary *dic, NSError *error) {
        if(error == nil)
        {
            NSLog(@"access code ==  %@ ",token);
        }
        else
        {       
            NSLog(@"Error ==  %@ ",error);
        }

    }];
}