【发布时间】:2012-06-05 14:35:58
【问题描述】:
我在我的项目中实现了 textToSpeech,并希望在说出文本时显示警报视图。这里我调用 textToSpeech 的方法:
//-----before TTS starts i try to display alertView with Cancelbutton
//[self performSelectorOnMainThread:@selector(alertWhileTTS) withObject:nil waitUntilDone:YES]; //gray view and no alertview
//[self performSelector:@selector(alertWhileTTS)]; //gray view and no alertview
//[self alertWhileTTS]; //gray view and no alertview
//this part here is blocking, no gray screen,
//after TTS is ready, the alertView is displayed
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI if you have to
[self alertWhileTTS];
});
[[self view] setNeedsDisplay];
[self synthesizeInBackground];
[queue waitUntilAllOperationsAreFinished];
[self setIsSpeaking: false];
[[self view] setNeedsDisplay];
这里是 synthesizeInBackground 方法(in 方法synthesize启动 TTS):
- (void) synthesizeInBackground {
queue = [[NSOperationQueue alloc] init];
operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(synthesize) object:nil];
[queue addOperation: operation];
}
在 TTS 时,我想显示带有 cancel 按钮的 alertView。但在我的情况下,我只得到一个没有 alertView 的灰屏。
如何正确调用 alertWhileTTS,以便显示 alertView?
这是alertWhileTTS的内容:
- (void) alertWhileTTS {
UIAlertView *inboxRead = [[[UIAlertView alloc] initWithTitle:@"Inbox tts..."
message:nil
delegate:self
cancelButtonTitle:@"Abbrechen"
otherButtonTitles:nil] autorelease];
inboxRead.tag = 997;
[inboxRead show];
}
更新查看我的解决方案,该解决方案有效:
[self performSelectorOnMainThread:@selector(alertWhileTTS) withObject:nil waitUntilDone:YES];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) {
[[self view] setNeedsDisplay];
[self synthesizeInBackground];
[queue waitUntilAllOperationsAreFinished];
[self setIsSpeaking: false];
[[self view] setNeedsDisplay];
});
【问题讨论】:
-
手动内存管理已经有一段时间了,但我认为您不应该这么快释放警报视图。尝试自动释放它。还要确保在主线程上执行此操作。所有 UI 更改都必须在此处完成。
标签: ios multithreading uialertview dispatch performselector