【问题标题】:Perform Action after UIAlertView is showing?UIAlertView 显示后执行操作?
【发布时间】:2013-06-07 19:13:59
【问题描述】:

我正在将图片上传到服务器。我正在使用 UIAlertView,AlertView1,它询问用户是否要上传图片。如果是,第二个 alertview,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.
}

【问题讨论】:

    标签: ios objective-c uialertview


    【解决方案1】:

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

    【讨论】:

    • 谢谢!不看委托方法真是太愚蠢了。 X_X 我只是依靠-(void)alertView...的xcode弹出窗口
    【解决方案2】:

    使用多线程将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
        });
    });
    

    【讨论】:

      【解决方案3】:

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

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

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

      【讨论】:

        猜你喜欢
        • 2017-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多