【问题标题】:ios - shoud UIAlertView be used in the returning block of NSUrlConnection?ios - 应该在 NSUrlConnection 的返回块中使用 UIAlertView 吗?
【发布时间】:2012-11-19 00:40:06
【问题描述】:

当我在 NSUrlConnection 的返回块中使用 UIAlertView 时,会发生一些罕见的崩溃。我不允许在异步线程中使用 UIAlertView 吗?

大多数时候,它似乎工作正常。

【问题讨论】:

  • @rmaddy 谢谢我试图向用户发出警报消息。我可以使用什么替代 UIAlertView?

标签: ios ios5 nsurlconnection uialertview


【解决方案1】:

所有与 UI 相关的代码都需要在主线程上工作。

当我在另一个线程上显示 alertView 时,我遇到了类似的崩溃。

您需要使用dispatch_asyncperformSelectorOnMainThread 显示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];

【讨论】:

    【解决方案2】:

    我认为你只能在主线程上使用 UIAlertView。 在您的返回块中使用performSelectorOnMainThread:<#(SEL)#> withObject:<#(id)#> waitUntilDone:<#(BOOL)#>

    【讨论】:

      猜你喜欢
      • 2017-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-26
      • 1970-01-01
      • 2012-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多