nsurlconnection在iOS中的选择器视图
问题描述:
我是新来的iOS我很努力地在使用nsurlconnection POST方法的选取器视图中显示值(id)的特定对象。当我单击按钮时使用nslog显示“id”的所有值,但不在选择器视图中。nsurlconnection在iOS中的选择器视图
编码
按钮动作:
-(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]);
}
答
我想你哒之前重装选择器组件ta从服务器到达。
你应该调用[self sendDataToServer : @"GET"];
这从viewdidload而不是按钮点击,所以当用户按下按钮,然后数据已经到达。我认为在你的案例中,数据来自服务器之前,重新加载选择器视图。
或者你可以把[self.pickerdata reloadAllComponents];
这个声明放在connectionDidFinishLoading
方法中作为最后一条语句。所以无论何时回应,它都会重新加载pickerview。你可以把这两行
[self.pickerdata reloadAllComponents];
[self.view addSubview:pickerdata];
到connectionDidFinishLoading
方法而不是buttonclick方法。所以当你点击按钮只是你的get
方法被调用,当响应来到它然后重新加载选择器和显示来查看,但它需要一些时间来显示选择器。所以更好的方法是当viewcontroller加载时在viewdidload中调用webservice,并在用户单击按钮之前以及用户单击重新加载数据并显示选取器之前准备好您的array
。
希望这将有助于:)
答
-(IBAction)sendDataUsingGet:(id)sender{
[self sendDataToServer : @"GET"];
//Show some loading views here
}
-(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;
//Hidden loading views
...
// Reloading data
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:pickerdata];
[self.pickerdata reloadAllComponents];
});
}
你不应该在新项目中使用NSURLConnection的分配新的数据,它的弃用。 – HAS