gpt4 book ai didi

ios - iOS 中选择器 View 中的 nsurlconnection

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:54 24 4
gpt4 key购买 nike

我是 iOS 的新手,我很努力地使用 nsurlconnection POST 方法在选择器 View 中显示值(id)的特定对象。当我单击按钮时,它使用 nslog 但不在选择器 View 中显示“id”的所有值。

编码

按钮操作:

-(IBAction)sendDataUsingGet:(id)sender{

[self sendDataToServer : @"GET"];
[self.pickerdata reloadAllComponents];
[self.view addSubview:pickerdata];

}

委托(delegate)方法:

-(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;

}

选择器 View :

-(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]);

}

最佳答案

我认为您在数据从服务器到达之前重新加载选择器组件。

您应该从 viewdidload 调用 [self sendDataToServer : @"GET"]; 而不是单击按钮,这样当用户按下按钮时数据已经到达。我认为在您的情况下,选择器 View 在数据来自服务器之前重新加载。

或者您可以将 [self.pickerdata reloadAllComponents]; 这条语句放在 connectionDidFinishLoading 方法中作为最后一条语句。因此,每当响应到来时,它就会重新加载 pickerview。你可以把这两行都放在

  [self.pickerdata reloadAllComponents];
[self.view addSubview:pickerdata];

connectionDidFinishLoading 方法而不是 buttonclick 方法。因此,当您单击按钮时,只会调用您的 get 方法,当响应到来时,它会重新加载选择器并显示以查看,但显示选择器需要一些时间。所以更好的方法是在加载 viewcontroller 时在 viewdidload 中调用 webservice,并在用户单击按钮之前和用户单击重新加载数据并显示选择器时准备好 array

希望这会有所帮助:)

关于ios - iOS 中选择器 View 中的 nsurlconnection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36856402/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com