2012-10-03 25 views
0

我有这样的代码的iOS UIAlertView中显示和处理顺序

- (IBAction)save:(id)sender { 

    self.imageView.image = [UIImage imageNamed:@"loading.gif"]; 

    [self.view setUserInteractionEnabled:NO]; 
    MBProgressHUD *hudSave = [MBProgressHUD showHUDAddedTo:self.navigationController.view animated:YES]; 
hudSave.labelText = @"Saving..."; 

    NSLog(@"save"); 

    UIAlertView *deco; 

    if (!isVideo) { 
     UIImageWriteToSavedPhotosAlbum (imageView.image, nil, nil , nil); 
     deco = [[UIAlertView alloc]initWithTitle:@"Save" message:@"Your photo has been saved." delegate: nil cancelButtonTitle:@"oK" otherButtonTitles:nil]; 
    }else{ 

     //UIAlertView *explain; 
     //explain = [[UIAlertView alloc]initWithTitle:@"Wait during processing" message:@"Your video is being filtered, this process may be long, depending of both video and device. Please do not close this app until task is finished." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
      UIAlertView *explain; 
      explain = [[UIAlertView alloc]initWithTitle:@"Wait during processing" message:@"Your video is being filtered, this process may be long, depending of both video and device. Please do not close this app until task is finished." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
      [explain show]; 

      [self InitialisationRefiltrage:urlVideo]; 

      //[self InitialisationRefiltrage:urlVideo]; 
     deco = [[UIAlertView alloc]initWithTitle:@"Save" message:@"Your video has been saved." delegate: nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; 
    } 

    [MBProgressHUD hideHUDForView:self.navigationController.view animated:YES]; 
    [deco show]; 

    [self.view setUserInteractionEnabled:YES]; 

} 

问题的部分是,如果我的文件是一个视频,它会直接太“initialisationRefiltrage”(谁是工作的罚款),但没有显示MBProgressHUD和警报视图解释,并在我的视频traitement后,它显示一切(解释,装饰,和MBProgressHUD)在同一时间。

我尝试一些派遣,线程等......但我认为一个不正确的做法,所以你可以给我一个线索,如何做到这一点。

祝您有愉快的一天。

回答

1

将代码[self InitialisationRefiltrage:urlVideo]置于UIAlertView的委托方法中,以便它被执行只有当警报已被显示并且用户点击警报的按钮时。

您也可以使用一些第三方UIAlertView子类,它们使用完成块来使代码仅在警报解除时执行。 See my class that does this for example

此外,您应该尊重编码约定,并使用以小写字母开头的方法名称来提高代码的可读性。

+0

你的子类很好地解决了我的问题,所以非常感谢你。现在我的警报和进度条都在顺畅且节奏地运行。我也修改函数的名称,通常我尊重编码惯例我不知道为什么不在这里,也许我很累,或者可能是复制粘贴我不检查好我不记得^^ – Arfost

1

用户界面在“run loop”中更新。

您正在调用的调用告诉iOS显示一些视图(alert,MUD ...),并且会在循环的下一次运行中执行此操作。

您需要做的是等待用户在继续之前响应警报。您可以通过设置自己的UIAlert的委托,然后响应事件做到这一点:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    ... 
} 

也有让你一个块传递给警报视图可用库,从而简化了整个事情。 (例如,https://github.com/jivadevoe/UIAlertView-Blocks

P.S.我看到你是Stack Overflow的新手 - 请在我的答案中打勾,如果你很高兴它回复了你的问题...

+0

我使用AliSoftware的类,但你的答案很好解释,我假设你建议的图书馆也做这项工作,所以非常感谢你。伤心,我不能接受这两个答案。 – Arfost

+0

但你仍然可以投票;) – tarmes

+0

我尝试,但显然我需要15个声望点投票了一些东西。 – Arfost