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

发布于 2024-12-01 00:32:48 字数 900 浏览 0 评论 0原文

我的问题是我有一个 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];
}

My problem is that i have an ActionSheet, which is disappearing from screen only when action under this button is completed. My problem is that i want to click 'save' inside my action sheet, then dismiss action sheet and then to show some alert, informing user to wait until saving is done. Currently it works different: firstly action sheet is shown, then there is saving message UNDER action sheet, finally action sheet is removed from view.. so user doesnt see any alert message.

How to dismiss actionSheet earlier than xcode does it?

Method under 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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸 2024-12-08 00:32:48

您应该将 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];
        });
    });
}

You should move your saveImageToCameraRoll method onto a separate thread, or at least asynchronously on the main thread. Then you can dismiss the alert and saveAction: can return before it completes.

The simplest way to do this would be using dispatch_async. Use dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) to get a queue for a separate thread, or dispatch_get_main_queue() for the main thread. Make sure not to do any UI work (or use any APIs which aren't thread-safe) on other threads!


Edit: more detail:

- (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];
        });
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文