UIAlertView在块内部崩溃

问题描述:

我有一个UIAlertView块,我想点击OK后运行一些代码。UIAlertView在块内部崩溃

- (IBAction)connectToAccount:(UIButton *)sender { 


     void (^block) (FTjsonRecords *obj, NSError *error) = ^(FTjsonRecords *obj, NSError *error) { 


       [self updateInterfaceWithReachability:self.hostReachability]; 

        UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Success!" 
                   message:@"Online sync is now enabled." 
                   delegate:self 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil]; 
        [av show]; 


     }; 

     [sync checkFirstLogin:email viaPassword:password viaCompletion:block]; 
    } 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    [[self navigationController] popToRootViewControllerAnimated:NO]; 
    NSNotification *note = [NSNotification notificationWithName:IOS_SWITCH_TAB object:nil]; 
    [[NSNotificationCenter defaultCenter] postNotification:note]; 
} 

当我点击确定,应用程序崩溃,没有任何例外跟进。 我不知道如何继续。建议会很好。由于

UPDATE:

我已经设置[av show];一个破发点的权利和螺纹看起来是这样的:

enter image description here

+3

该块是否在主队列/线程上执行? –

+0

@斯科特我相信它是。请参阅最新的问题。谢谢 – Houman

+0

完成块是否通过复制正确地移动到堆中? – Sulthan

因为所有的UI代码必须在主线程中运行,您需要将您的警报代码附在dispatch_async区块中,如下所示:

dispatch_async(dispatch_get_main_queue(), ^{ 
    UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Success!" 
               message:@"Online sync is now enabled." 
               delegate:self 
             cancelButtonTitle:@"OK" 
    [av show]; 
}); 
+0

感谢您的提示。我只是试图没有任何成功。这是和以前一样的问题。当我点击OK时,它崩溃了。 – Houman

+0

因此它必须在'alertView:didDismissWithButtonIndex:'中的某处崩溃。你有没有检查过你的'note'对象是不是零? – Macondo2Seattle

+0

好吧,崩溃显然是在'FTLoginViewController connectToAccount:''中。我不认为警报视图在这里是一个问题。你可以发布'connectToAccount'的代码吗? – Macondo2Seattle

我终于找到了问题。因为它被认为是一个线程问题,我不能先发现它。

在行:

[self updateInterfaceWithReachability:self.hostReachability]; 

我正在做的一个异步线程一些工作,并在那年底,我叫

[[self navigationController] popToRootViewControllerAnimated:YES]; 

这是当我展示弹出的发生两人相撞。我从updateInterfaceWithReachability中删除了popToRootViewControllerAnimated,并将其放入UIAlertView的委托中,现在它按预期工作。

我希望这可以帮助其他人,以类似的情况。

另外值得考虑的是UIAlertView的内存管理。如果在可见状态下重新分配UIAlertView,它也会导致崩溃。