gpt4 book ai didi

ios - 如何使用异步 NSURLConnection 将数据传递给 View Controller

转载 作者:行者123 更新时间:2023-11-29 12:15:22 24 4
gpt4 key购买 nike

我有 View Controller,我可以从 Web 获取数据,解析 Json,并将字符串传递给另一个 View Controller。如果我使用同步 NSURLConnection,一切正常。

但是如果我切换到异步,那么方法 (void)prepareForSegue:(UIStoryboardSegue *) 在解析我从 web 获取的 Json 数据之前调用。

直接跳过_jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]方法。有什么想法吗?预先感谢您的帮助。这是我的代码:

-(void)getClothInfo {
NSString *allowedClothSizeToServer = [_foreignSizeToServer stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *getDataURL = [NSString stringWithFormat:@"http://xsdcompany.com/jsoncloth.php?foreignSize=%@",allowedClothSizeToServer];
NSURL *url = [NSURL URLWithString:getDataURL];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {

if (connectionError) {
[self showAlertWithMessage2:@"Server is Unavialable"];
} else {
_jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//Loop trough our jsonArray
for (int i=0; i<_jsonArray.count; i++) {
//Create our size object
_usSizeFromServer = [[_jsonArray objectAtIndex:i] objectForKey:@"usSizeCloth"];
}
}
}];
}

- (IBAction)getIt:(id)sender {
// Validate data
if ([self validData] == NO)
{
return;
}

[self getClothInfo];
[self showNextViewController];
}

-(void) showNextViewController {
[self performSegueWithIdentifier:@"GetCLothInfo" sender:nil];
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ResultViewController *resultViewController = [segue destinationViewController];
resultViewController.foreignSizeToResult = [[NSString alloc] initWithFormat:@"%@ size for %@ is %@", [_pickerProcessor selectedCountry].countryName, [_pickerProcessor selectedCloth].clothName, [_pickerProcessor selectedSize].sizeName];
resultViewController.dataForUsSize = [[NSString alloc] initWithFormat:@"Your US size for %@ is %@", [_pickerProcessor selectedCloth].clothName, _usSizeFromServer];
}

最佳答案

您有两个选择。您可以从 getClothInfo 方法内的完成 block 调用 showNextViewController。或者更好的是,将完成 block 参数添加到您的 getClothInfo 方法,并从 NSURLConnection 的完成 block 中调用它。

像这样:

-(void)getClothInfo:(void ^(void))completion {
NSString *allowedClothSizeToServer = [_foreignSizeToServer stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *getDataURL = [NSString stringWithFormat:@"http://xsdcompany.com/jsoncloth.php?foreignSize=%@",allowedClothSizeToServer];
NSURL *url = [NSURL URLWithString:getDataURL];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"GET"];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler: ^(NSURLResponse *response, NSData *data, NSError *connectionError) {

if (connectionError) {
[self showAlertWithMessage2:@"Server is Unavialable"];
} else {
_jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

//Loop trough our jsonArray
for (int i=0; i<_jsonArray.count; i++) {
//Create our size object
_usSizeFromServer = [[_jsonArray objectAtIndex:i] objectForKey:@"usSizeCloth"];
}

if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
completion();
});
}
}
}];
}

- (IBAction)getIt:(id)sender {
// Validate data
if ([self validData] == NO)
{
return;
}

[self getClothInfo:^ {
[self showNextViewController];
}];
}

关于ios - 如何使用异步 NSURLConnection 将数据传递给 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32150250/

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