在alertview中显示Exc_Bad_Access

时间:2012-10-26 09:11:35

标签: iphone objective-c xcode crash exc-bad-access

在UIAlertView显示消息中获取Exc_Bad_Access。

UIAlertView *systemAlert1 = [[UIAlertView alloc]initWithTitle:@"System message" message:@"note" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [systemAlert1 show];  **//Crashing on this line [EXC_BAD_ACCESS]**
    [systemAlert1 release]; 

为什么我会这样?请帮忙

2 个答案:

答案 0 :(得分:1)

任何UI内容,包括警报的显示都应在主线程上完成。 如果你在其他线程上执行此操作,它肯定会崩溃。

答案 1 :(得分:0)

可能是因为您的警报是从后台线程而不是主线程调用的。建议仅在主线程上进行与用户界面相关的更改,以避免应用程序的这种行为

试试这段代码:

UIAlertView *systemAlert1 = [[UIAlertView alloc]initWithTitle:@"System message" message:@"note" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[systemAlert1 performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES]; 
[systemAlert1 release]; 

希望这会有所帮助。如果您还有其他需要,请告诉我。

相关问题