我的问题是我有一个 ActionSheet,仅当此按钮下的操作完成时,它才会从屏幕上消失。我的问题是我想在我的操作表中单击“保存”,然后关闭操作表,然后显示一些警报,通知用户等待保存完成。目前它的工作方式不同:首先显示操作表,然后在操作表下保存消息,最后从 View 中删除操作表..所以用户看不到任何警告消息。
如何在 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];
}
最佳答案
您应该将您的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];
});
});
}
关于objective-c - 在按钮下的操作完成之前关闭 actionSheet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7144010/