2016-04-27 60 views
0

.h file 我是iOS新手我在使用POST方法传递url链接后得到响应我无法显示在选择器视图中。为POST在iOS中的响应后,选择器视图为空

方法:对于选择器视图

-(void) sendDataToServer : (NSString *) method{ 
     NSString *[email protected]"3"; 

     NSURL *url = nil; 
     NSMutableURLRequest *request = nil; 
     if([method isEqualToString:@"GET"]){ 

      NSString *getURL = [NSString stringWithFormat:@"%@?branch_id=%@", URL, Branchid]; 
      url = [NSURL URLWithString: getURL]; 
      request = [NSMutableURLRequest requestWithURL:url]; 
      NSLog(@"%@",getURL); 

     }else{ // POST 

      NSString *parameter = [NSString stringWithFormat:@"branch_id=%@",Branchid]; 
      NSData *parameterData = [parameter dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 

      url = [NSURL URLWithString: URL]; 
      NSLog(@"%@", parameterData); 
      request = [NSMutableURLRequest requestWithURL:url]; 
      [request setHTTPBody:parameterData]; 

      arr= [NSMutableString stringWithUTF8String:[parameterData bytes]]; 
     } 

     [request setHTTPMethod:method]; 
     [request addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
     NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

     if(connection) 
     { 
      mutableData = [NSMutableData new]; 
      } 

    } 

Delegate method for POST: 


-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [mutableData setLength:0]; 
} 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [mutableData appendData:data]; 
} 

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
     return; 
} 

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 


    NSError* error; 
    json = [NSJSONSerialization JSONObjectWithData: mutableData 
              options:kNilOptions 
              error:&error]; 

    responseArray = [json valueForKeyPath:@"BranchByList.currency"]; 
    NSLog(@"%@",responseArray); 
    } 

委托方法:

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{ 
    return 1; 
} 
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;{ 
     return [responseArray count]; 
    } 

-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;{ 
    return [responseArray objectAtIndex:row]; 
} 

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; 
{ 
    NSLog([responseArray objectAtIndex:row]); 

} 

我收到响应和在的NSLog但在选择器查看其示出空单line.I别显示不要在哪里出错,请帮助我。 pickerview datasource and delegate connection

+0

重装在connectionDidFinishLoading你的选择器视图 – Kalpesh

+0

重装不可见其表示的NSArray @Kalpesh没有可视化界面 –

+1

'[myPickerView reloadAllComponents]''中connectionDidFinishLoading' –

回答

0

创建选择器视图的一个出口和在connectionDidFinishLoading重新装入

[拾取器reloadAllComponents];

相关问题