在其按钮完成后的操作之前关闭actionSheet

时间:2011-08-22 06:52:21

标签: objective-c ios cocoa-touch uiactionsheet dismiss

我的问题是我有一个ActionSheet,只有当此按钮下的操作完成时才会从屏幕上消失。我的问题是我想在我的操作表单击“保存”,然后关闭操作表,然后显示一些警报,通知用户等待保存完成。目前它的工作方式不同:首先显示操作表,然后保存消息UNDER操作表,最后从视图中删除操作表..所以用户没有看到任何警报消息。

如何在xcode之前解决actionSheet呢?

sheetActionButton下的方法:

- (IBAction)saveAction:(id)sender
{
UIAlertView *alert;
alert = [[[UIAlertView alloc] initWithTitle:@"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
[alert show];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
[indicator startAnimating];
[alert addSubview:indicator];
[indicator release];

[self saveImageToCameraRoll];

[alert dismissWithClickedButtonIndex:0 animated:YES];
}

1 个答案:

答案 0 :(得分:2)

您应该将saveImageToCameraRoll方法移动到单独的线程上,或者至少在主线程上异步移动。然后你可以解除警报,saveAction:可以在完成之前返回。

最简单的方法是使用dispatch_async。使用dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)获取单独线程的队列,或使用dispatch_get_main_queue()作为主线程。确保不要在其他线程上执行任何UI工作(或使用任何非线程安全的API)!


编辑:更多细节:

- (IBAction)saveAction:(id)sender {
    UIAlertView *alert;
    alert = [[[UIAlertView alloc] initWithTitle:@"Saving photo to library\nPlease Wait..." message:nil delegate:self cancelButtonTitle:nil otherButtonTitles: nil] autorelease];
    [alert show];
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    indicator.center = CGPointMake(alert.bounds.size.width / 2, alert.bounds.size.height - 50);
    [indicator startAnimating];
    [alert addSubview:indicator];
    [indicator release];

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // Save in the background
        [self saveImageToCameraRoll];
        dispatch_async(dispatch_get_main_queue(), ^{
            // Perform UI functions on the main thread!
            [alert dismissWithClickedButtonIndex:0 animated:YES];
        });
    });
}