我有一个正在等待连接的应用程序。当应用程序正在等待时,我需要向用户显示 AlertView,它应该在一段时间后以编程方式或通过用户单击 AlertView 的取消按钮关闭。
主线程正在等待客户端连接,而在另一个线程中,我正在创建并显示正在更新对话框时间的 AlertView。该线程有一个 NSRunLoop,它更新 AlertView 上的文本。
一切正常,除了 AlertView 没有接收到触摸事件,甚至它没有以编程方式被关闭。谁能阐明我在这里可能做错了什么。
这是示例代码。
funcA() {
[NSThread detachNewThreadSelector:@selector(createDialog) toTarget:self withObject:nil];
[NSThread detachNewThreadSelector:@selector(updateDialog) toTarget:self withObject:nil];
..
..
BlockingWait();
..
..
}
- (void) createDialog {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
alert = [[UIAlertView alloc] initWithTitle:@"Wait" message:@"\n" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
...
label = [[UILabel alloc] initWithFrame:CGRectMake(30.0f, 20.0f, 225.0f, 90.f)];
[alert show];
[alert release];
[pool drain];
}
- (void) upDateDialog {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSRunLoop *loop = [NSRunLoop currentRunLoop];
timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateText) userInfo:nil repeats:YES];
[loop run];
[pool drain];
}
- (void) updateText {
label.text = " Wait for" + n + "secs";
n--;
if ( n == 0 )
[alert dismissWithClickedButtonIndex:0 animated:YES]; // Doesn't work
}
- (void) alertView: (UIAlertView *) alert clickedButtonAtIndex:(NSInteger) buttonIndex {
// Never Gets called
NSLog(@"Alert is dissmissed with button index %d", buttonIndex);
if (buttonIndex == 0) {
[timer invalidate];
}
}
最佳答案
我什至没有读过你的代码,只是标题已经表明你真的应该在你的架构上工作。您可以尝试破解 UIKit,但它被设计为仅由 UI 线程管理。因此,将阻塞调用移到线程中,并从主线程管理 UIAlertView。
关于ios - 当主线程在 IOS 中被阻塞时,UIAlertView 在线程中关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8882393/