如何正确搭配?不显示UIAlertView,仅显示灰屏 [英] How to multithred correctly? UIAlertView is not displayed, only gray screen
问题描述
我在我的项目中实现了textToSpeech,并希望在说文本时显示警报视图.在这里,我正在调用textToSpeech的方法:
i have implemented textToSpeech in my project and want to display an alertview while text is spoken. here i am calling the methods for 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方法(在方法中为 synthesize
启动TTS ):
Here the synthesizeInBackground method(in method synthesize
starts the TTS):
- (void) synthesizeInBackground {
queue = [[NSOperationQueue alloc] init];
operation = [[NSInvocationOperation alloc] initWithTarget:self
selector:@selector(synthesize) object:nil];
[queue addOperation: operation];
}
在TTS中,我想显示带有cancel
按钮的alertView.但是在我的情况下,我只有一个没有alertView的灰色屏幕.
While TTS i want to display an alertView with cancel
button. But in my case i am only getting a gray screen without the alertView.
如何正确调用alertWhileTTS ,以便显示alertView?
How can i call alertWhileTTS correctly, so the alertView gets displayed?
这是alertWhileTTS的内容:
here is the content of alertWhileTTS:
- (void) alertWhileTTS {
UIAlertView *inboxRead = [[[UIAlertView alloc] initWithTitle:@"Inbox tts..."
message:nil
delegate:self
cancelButtonTitle:@"Abbrechen"
otherButtonTitles:nil] autorelease];
inboxRead.tag = 997;
[inboxRead show];
}
更新,查看有效的解决方案:
UPDATE see my solution, that works:
[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];
});
推荐答案
更改alertWithTTsTo
Change the alertWithTTsTo
UIAlertView *inboxRead = [[[UIAlertView alloc] initWithTitle:@"Inbox tts..."
message:nil
delegate:self
cancelButtonTitle:@"Abbrechen"
otherButtonTitles:nil] autoRelease];
inboxRead.tag = 997;
[inboxRead show];
也不要忘记从主ui线程调用函数alertWhileTTS
通过做
Also dont forget to call the function alertWhileTTS
from the main ui thread
By doing
dispatch_async(dispatch_get_main_queue(), ^{
//Update UI if you have to
[self alertWhileTTS];
});
这篇关于如何正确搭配?不显示UIAlertView,仅显示灰屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!