0

我正在将图片上传到服务器。我正在使用 UIAlertView,AlertView1,它询问用户是否要上传图片。如果是,第二个警报视图,AlertView2 将显示一个进度条并在上传完成后消失。

因此,一旦用户在 AlertView1 中单击“是”,我就会调用 show AlertView2 并调用方法 [self uploadPhoto]。所以问题来了,即使在 AlertView2 有时间显示之前,CPU 密集型 uploadPhoto 正在运行,它会延迟 AlertView2 显示几秒钟。似乎 AlertView2 终于显示了上传过程的方法。

如何仅在 AlertView2 显示后才开始上传过程?

这是代码

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
      //Detectss AlertView1's buttonClick 
      [self.alertView2 show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{

     //Launches when AlertView1 is dismissed
     [self UploadPhoto]
 }

 -(void)uploadPhoto
{
     //CPU/Network Intensive Code to Upload a photo to a server.
}
4

3 回答 3

3

将您的控制器设置为第二个警报视图的代理,并在您收到第二个警报的 didPresentAlertView: 消息时上传照片。

于 2013-06-07T19:22:24.293 回答
0

使用计时器[self uploadPhoto]调用后调用您的方法-[self.alertView2 show]

[self performSelector:@selector(uploadPhoto) withObject:nil afterDelay:0.3];

它会在您的警报出现 3 秒后调用您的方法。

于 2013-06-07T19:26:34.013 回答
0

使用多线程调度uploadPhoto到另一个线程。这样您就不会在执行昂贵的网络操作时阻塞主线程。

dispatch_queue_t queue = dispatch_queue_create("upload queue", NULL);
dispatch_async(queue, ^{
    [self uploadPhoto];
    dispatch_async(dispatch_get_main_queue(), ^{
        //dismiss alertview2 here
    });
});
于 2013-06-07T19:30:44.477 回答