我有这段代码尝试从Web服务提供数组。如果失败,则会显示一条错误消息,可以点按它以重试。但是当我点击它时,错误消息不会消失,并且微调器不会重新出现。
-(void)Load_Commandes{
if(LIB_Error)
{
[LIB_Error removeFromSuperview];
LIB_Error = nil;
}
TABLE_Commandes.hidden = YES;
LIB_Chargement.hidden = NO;
spinner.hidden = NO;
[spinner startAnimating];
tab_Cdes = [self Init_Tableau];
spinner.hidden = YES;
LIB_Chargement.hidden = YES;
if(tab_Cdes != nil)
{
TABLE_Commandes.hidden = NO;
[TABLE_Commandes reloadData];
}
else
{
LIB_Error = [[UILabel alloc] initWithFrame:self.view.frame];
[LIB_Error setTextColor:[UIColor grayColor]];
[LIB_Error setBackgroundColor:[UIColor clearColor]];
[LIB_Error setTextAlignment:NSTextAlignmentCenter];
[LIB_Error setText:@"Erreur de chargement. \nToucher pour réessayer."];
[LIB_Error setNumberOfLines:2];
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Load_Commandes)];
[LIB_Error addGestureRecognizer:gesture];
LIB_Error.userInteractionEnabled = YES;
[self.view addSubview:LIB_Error];
}
}
以下是我在[self Init_Tableau]中调用网络服务的代码。
NSString *str=[NSString stringWithFormat:URL_ORDERS];
NSURL *url=[NSURL URLWithString:str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error: &error];
感谢您的帮助,因为我会变得疯狂! ^^
更新:我尝试了很多不同的方法。但这似乎是不可能的。无论我做什么,只有在完成所有治疗后才会应用视觉变化。
这是Init_Tableau的代码。
-(NSArray*)Init_Tableau
{
NSMutableArray* a = [NSMutableArray array];
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
NSLog(@"Erreur de chargement 1");
//return nil;
}
NSURLResponse* response;
NSError* error;
NSString *str=[NSString stringWithFormat:URL_ORDERS];
NSURL *url=[NSURL URLWithString:str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error: &error];
NSString *responseBody = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
if(error)
{
NSLog(@"Erreur de chargement 2");
return nil;
}
NSError *e = nil;
NSArray* resultList = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:&e];
for(NSDictionary* dico in resultList){
Entete* cde = [[Entete alloc] init];
cde.ncde = [dico objectForKey:NCDE];
cde.nomf = [dico objectForKey:NOMF];
cde.noml = [dico objectForKey:NOML];
cde.prenomf = [dico objectForKey:PRENOMF];
cde.prenoml = [dico objectForKey:PRENOML];
cde.adresse1f = [dico objectForKey:ADRESSE1F];
cde.adresse1l = [dico objectForKey:ADRESSE1L];
cde.adresse2f = [dico objectForKey:ADRESSE2F];
cde.adresse2l = [dico objectForKey:ADRESSE2L];
cde.cpf = [dico objectForKey:CPF];
cde.cpl = [dico objectForKey:CPL];
cde.villef = [dico objectForKey:VILLEF];
cde.villel = [dico objectForKey:VILLEL];
cde.phonef = [dico objectForKey:PHONEF];
cde.phonel = [dico objectForKey:PHONEL];
cde.societef = [dico objectForKey:SOCIETEF];
cde.societel = [dico objectForKey:SOCIETEL];
cde.etatCde = [dico objectForKey:ETATCDE];
cde.moyenPaiement = [dico objectForKey:MOYENPAIEMENT];
cde.fdp = [dico objectForKey:FDP];
cde.mnt = [[dico objectForKey:MNTCDE] floatValue];
[a addObject:cde];
}
return [NSArray arrayWithArray:a];
}