【问题标题】:Dismiss actionSheet before action under it's button is completed在其按钮下的操作完成之前关闭 actionSheet
【发布时间】:2011-10-31 22:07:39
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 我已尝试按照您的建议进行修改:dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{[self saveImageToCameraRoll];}); -> 现在它的工作方式完全不同,但仍然是错误的:actionSheet 被解除,但在 dispatch_async 完成工作后显示警报 o.O 为什么会这样..?我想要关闭 actionSheet、显示警报、保存操作、关闭警报。 Atm 它正在关闭 actionSheet、保存操作、显示警报、关闭警报,即使在我编写的代码中也不同。
    • 根据一些教程对其进行了更改: dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self saveImageToCameraRoll]; dispatch_async(dispatch_get_main_queue(), completion); });其中completion 是一个方法参数:completion:(void (^)(void))completion 并且我在调用时向它发送一个NULL。我在之前评论中描述的问题仍然存在。
    • 抱歉。您需要将[alert dismissWithClickedButtonIndex:0 animated:YES]; 移动到dispatch_async(dispatch_get_main_queue(), ^{ ... }); 块内。
    • 现在这是一个永无止境的故事;)警报永远不会被解雇:/我正在尝试修改代码来修复它
    • 知道了:必须删除:dispatch_async(dispatch_get_main_queue(), completion);现在作为一个魅力!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2013-02-02
    • 1970-01-01
    • 2017-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    • 2022-01-24
    相关资源
    最近更新 更多