【发布时间】:2016-04-26 06:00:48
【问题描述】:
我是 iOS 新手,我正在努力使用 nsurlconnection POST 方法在选取器视图中显示特定对象的值(id)。当我单击按钮时,它会使用 nslog 但不在选取器视图中显示“id”的所有值。
编码
按钮操作:
-(IBAction)sendDataUsingGet:(id)sender{
[self sendDataToServer : @"GET"];
[self.pickerdata reloadAllComponents];
[self.view addSubview:pickerdata];
}
委托方法:
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSMutableString *responseStringWithEncoded = [[NSMutableString alloc] initWithData: mutableData encoding:NSUTF8StringEncoding];
//NSLog(@"%@",responseStringWithEncoded );// this nslog will display all d response....
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData: mutableData
options:kNilOptions
error:&error]; //Now we got top level dictionary
arry = [json valueForKeyPath:@"BranchByList.id"];
NSLog(@"%@",arry);
NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[responseStringWithEncoded dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
serverResponse.attributedText = attrStr;
}
选择器视图:
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;{
return [arry count];
//[self.pickerdata reloadAllComponents];
}
-(NSString*) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;{
return [[arry objectAtIndex:row] valueForKey:@"currency"];
//[self.pickerdata reloadAllComponents];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
{
NSLog([arry objectAtIndex:row]);
}
【问题讨论】:
-
你不应该在新项目中使用 NSURLConnection,它已经被弃用了。
标签: ios objective-c nsurlconnection