2016-10-17 82 views
0

我已經從服務器使用以下代碼獲取數據。現在我想反映相同的其他視圖控制器有表視圖cells.How可以傳遞數據從這個singleton類到類有Table View cells。請幫助一些代碼在目標c.Thanks提前!數據從塊傳遞到視圖控制器

-(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; 
} 
+0

只是清楚你的疑問句「使具有表視圖單元中的數據」,這裏有很多方法可以從一個數據傳遞到另一個視圖。 – vaibhav

+0

我有一個視圖controllerA,它有它的tableview和一個類NetworkManger,我已經定義了上述方法。我得到了來自服務器的響應,但我無法在ViewController AI上的表視圖的單元格上反映此響應我是一名新開發人員,所以需要您的代碼幫助。 – FreshStar

+0

檢查這個答案https://gist.github.com/jitendrabenzatine/31c0e6171f8df8a878a363a5d240ebf7 –

回答

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); 
     } 

    }]; 
} 
+0

在調用函數中,什麼是sendLogin? – FreshStar

+0

這並不反映對錶格視圖單元格的響應詞典。由於ViewcontrollerB首先獲取加載,並且稍後返回responseDictionary,因此行數將變爲0. – FreshStar

+0

@Lovely:had u tried reload table or insert row in主線程? – Monish

相關問題