【发布时间】:2012-11-19 00:40:06
【问题描述】:
当我在 NSUrlConnection 的返回块中使用 UIAlertView 时,会发生一些罕见的崩溃。我不允许在异步线程中使用 UIAlertView 吗?
大多数时候,它似乎工作正常。
【问题讨论】:
-
@rmaddy 谢谢我试图向用户发出警报消息。我可以使用什么替代 UIAlertView?
标签: ios ios5 nsurlconnection uialertview
当我在 NSUrlConnection 的返回块中使用 UIAlertView 时,会发生一些罕见的崩溃。我不允许在异步线程中使用 UIAlertView 吗?
大多数时候,它似乎工作正常。
【问题讨论】:
标签: ios ios5 nsurlconnection uialertview
所有与 UI 相关的代码都需要在主线程上工作。
当我在另一个线程上显示 alertView 时,我遇到了类似的崩溃。
您需要使用dispatch_async 或performSelectorOnMainThread 显示alertView。
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
});
或
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title"
message:@"Message"
delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntillDone:NO];
【讨论】:
我认为你只能在主线程上使用 UIAlertView。
在您的返回块中使用performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>。
【讨论】: