1

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

如何在 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];
}
4

1 回答 1

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];
        });
    });
}
于 2011-08-22T06:57:19.633 回答