我的以下代码工作正常,但当我添加UIAlertView
时,它开始给我exc_bad_access
并且应用程序被挂起,
主要想法是在时间戳被检测时显示警告,
任何提示我做错了什么?
- (void) onPayload:(PayloadEvent *) event {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *header = @"[OnPayload] ";
if (event.payloadType == TYPE_IDENTIFIED) {
if ((event.contentID != -1) && (event.timeStamp == -1)) {
[mUI performSelectorOnMainThread: @selector(Trace:) withObject:[NSString stringWithFormat:@"%@ StaticID detected: %x\t\tConfidence: %f\n", header,(int)event.contentID, event.confidence] waitUntilDone:NO];
}
if ((event.timeStamp != -1) && (event.contentID == -1)) {
[mUI performSelectorOnMainThread: @selector(Trace:) withObject:[NSString stringWithFormat:@"%@ Timestamp detected: %f\t\tConfidence: %f\n", header, event.timeStamp, event.confidence] waitUntilDone:NO];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Poll" message:@"MY alert Poll" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
}
else if (event.payloadType == TYPE_NOT_IDENTIFIED) {
[mUI performSelectorOnMainThread: @selector(Trace:) withObject:[NSString stringWithFormat:@"%@ Content not marked\n", header] waitUntilDone:NO];
}
else if (event.payloadType == TYPE_MARKED_BUT_NOT_IDENTIFIED) {
[mUI performSelectorOnMainThread: @selector(Trace:) withObject:[NSString stringWithFormat:@"%@ Content marked but not identified\n", header] waitUntilDone:NO];
}
[event release];
[pool release];
}
我的错误出现在这里
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Poll" message:@"MY alert Poll" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show]; <<=====
[alert release];
答案 0 :(得分:2)
应该从主线程调用任何更改UI的代码:
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Poll" message:@"MY alert Poll" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
});
与某些评论相反,显示提醒的视图将保留UIAlertView
个实例,因此无需拨打autorelease
而非release
。
答案 1 :(得分:1)
考虑到您使用相同的方法多次调用-performSelectorOnMainThread
,我想它是在后台线程上调用的。 UI更改必须在主线程上发生,因此,您应该在主线程上调用[alert show];
以绕过错误。
[alert performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];