在 iOS 中响应后选择器视图为空
picker view empty after response in iOS
我是 iOS 的新手 我正在通过 url link 获得响应后使用 POST 方法 我无法在选择器视图中显示.
POST 的方法:
-(void) sendDataToServer : (NSString *) method{
NSString *Branchid=@"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 不知道哪里出了问题请帮助我。
创建一个选择器视图的 Outlet 并在 connectionDidFinishLoading 中重新加载它
[选择器重新加载所有组件];
POST 的方法:
-(void) sendDataToServer : (NSString *) method{
NSString *Branchid=@"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 不知道哪里出了问题请帮助我。
创建一个选择器视图的 Outlet 并在 connectionDidFinishLoading 中重新加载它
[选择器重新加载所有组件];