2011-08-22 27 views
1

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

如何解除actionSheet比xcode早吗? 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]; 
} 

回答

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]; 
     }); 
    }); 
} 
+0

我已经试过你的建议修改此:dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ {[自saveImageToCameraRoll];}); - >现在它工作完全不同,但仍然是错误的:actionSheet被解雇,但警报显示后dispatch_async完成工作o.O为什么这样..?我想要解雇actionSheet,显示警报,保存操作,关闭警报。 Atm它正在解雇actionSheet,保存行动,显示警报,甚至在代码中关闭警报,我写它不同。 – Vive

+0

是的,就像那样。 – jtbandes

+0

根据一些教程改变了它:dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^ { [自saveImageToCameraRoll]; dispatch_async(dispatch_get_main_queue(),完成); });完成是一个方法参数:完成:(void(^)(void))完成,我向它发送一个NULL时调用。我在之前评论中描述的问题仍然存在。 – Vive

相关问题