【问题标题】:UIAlertView makes the program crashUIAlertView 使程序崩溃
【发布时间】:2013-09-25 12:52:01
【问题描述】:

我遇到了崩溃:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIKeyboardTaskQueue performTask:] may only be called from the main thread.'

我 2 天都找不到解决方案。 这是代码:

[alert dismissWithClickedButtonIndex:0 animated:YES];
UIAlertView *noTicketAlert = [[UIAlertView alloc] initWithTitle:@"Aradığınız kriterlere uygun bilet bulunamadı!" message:nil delegate:self cancelButtonTitle:@"Tamam" otherButtonTitles: nil];
[noTicketAlert show];

【问题讨论】:

  • 愿意向我们展示您的警报视图代码吗?
  • 这应该在 Xcode 4 和 Xcode 3 中崩溃了。
  • 我认为错误信息不会更明确。
  • 它是 xcode 5,程序正在运行 xcode 3 和 4。我尝试将我的应用程序升级为 ios7,但 uialertview 使我的应用程序崩溃
  • 您似乎正在使用两个警报实例。能给个完整的代码吗?

标签: ios7 uialertview


【解决方案1】:

我通过尝试显示来自后台线程的警报触发了此错误。像这样修复:

dispatch_async(dispatch_get_main_queue(), ^{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:...
    [alertView show];
});

【讨论】:

    【解决方案2】:

    正常显示 UIAlertView 时出现此错误(没有有趣的按钮覆盖内容)。事实证明,我连续两次展示了它。我的解决方法是删除错误的重复调用。

    如果您确实需要同时显示两个警报视图,并且您收到此错误,那么有效的修复(并解决错误消息本身)是在主线程上运行代码:

    [[NSOperationQueue mainQueue] addOperationWithBlock:^
        {
        // Your code that presents the alert view(s)
        }];
    

    【讨论】:

      【解决方案3】:

      是的,我找到了解决方案,并与大家分享。 我试图覆盖 dismissWithClickedButtonIndex 函数,并发送了唯一的 buttonIndexes 就像9999 我的每个警报一样。 也就是说,

      - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
          [self viewWillDisappear:YES];
          if(buttonIndex == 9999) {
              noTicketAlert = [[UIAlertView alloc] initWithTitle:@"Aradığınız kriterlere uygun bilet bulunamadı!" message:nil delegate:self cancelButtonTitle:@"Tamam" otherButtonTitles: nil];
              [noTicketAlert show];
          }
      }
      

      如果我想显示 noticketAlert,我将这个方法称为:

      [alert dismissWithClickedButtonIndex:9999 animated:YES];
      

      【讨论】:

        【解决方案4】:

        如果您有自定义按钮,请确保实现委托方法:

        - (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
        {
            return YES;
        }
        

        如果没有找到这个选择器,那么程序就会崩溃..

        【讨论】:

          【解决方案5】:

          对于那些寻找 Swift 2 解决方案的人,我遇到了类似的问题,并使用 @Dave Batton 的解决方案解决了它

          dispatch_async(dispatch_get_main_queue(), {
              self.performSegueWithIdentifier("loginSegue", sender: self)
          })
          

          【讨论】:

            猜你喜欢
            • 2012-11-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-04-07
            • 2015-06-14
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多