UIAlertView中,使程序崩溃

问题描述:

我有一个崩溃:UIAlertView中,使程序崩溃

*** 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]; 
+0

关怀,向我们展示你的代码警报视图? –

+1

这应该已经在Xcode 4和Xcode 3中崩溃了。 – *foe

+2

我不认为错误消息可能更加明确。 – *foe

是的,我已经找到了解决办法和我分享与你们。 我尝试覆盖dismissWithClickedButtonIndex函数,并为每个警报发送了唯一的按钮索引 ,如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]; 

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

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

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

当我通常提出一个UIAlertView(没有有趣的按钮覆盖的东西)时,我得到了这个错误。事实证明,我连续两次呈现它。在我的情况下,解决方法是删除错误的重复呼叫。

如果您确实需要在接近同一时间显示两个警报视图,并且出现此错误,则可以使用(并且解决错误消息本身)的修复程序在主线程上运行代码:

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

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

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

对于那些寻找雨燕2回答这个问题,我遇到了类似的问题,并与@戴夫BATTON的解决方案解决了这个问题

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