我会尝试使用调度信号量。将其保存在实例变量中。
@interface MyClass ()
{
dispatch_semaphore_t dsema;
}
@end
然后,在后台线程方法中:
// this is the background thread where you are processing the archive files
- (void)processArchives
{
...
self.dsema = dispatch_semaphore_create(0);
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"Title"
...
delegate: self
...
];
[alertView show];
});
dispatch_semaphore_wait(self.dsema, DISPATCH_TIME_FOREVER);
// --> when you get here, the user has responded to the UIAlertView <--
dispatch_release(self.dsema);
...
}
将UIAlertView
调用此委托方法:
// this is running on the main queue, as a method on the alert view delegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// do stuff with alertView
if (buttonIndex == [alertView firstOtherButtonIndex]) {
...
// when you get the reply that should unblock the background thread, unblock the other thread:
dispatch_semaphore_signal(self.dsema);
...
}
}