UIAlertView 报 EXC_BAD_ACCESS

今天再写程序的时候,弹出UIAlertView 调用show方法的时候竟然报了 EXC_BAD_ACCESS的错误。
肯定不是先release 了,再调用的情况
实在是无法理解,随即把代码copy到一个干净的工程里跑了下,完全没有问题,然后突然想起来,只有主线程才有权限更改UI,这个UIAlertView会不会也是属于UI的更新呢,于是调用方法 performSelectorOnMainThread 方法,去主线程掉用显示UIAlertView的方法,果然好了。
记录下来,怕以后忘了。

下面是代码,这个弹出的UIAlertView是会自动关掉的
- (void) showAutoClearMessage:(NSString*) message
{
    
    if ([NSRunLoop currentRunLoop] != [NSRunLoop mainRunLoop]) {
        NSLog(@"<< perform in main thread>>");
        [self performSelectorOnMainThread:@selector(showAutoClearMessage:) withObject:message waitUntilDone:NO];
        return;
    }

    autoClearAlert = [[UIAlertView alloc]
                 initWithTitle:@"" message:message
                 delegate:self cancelButtonTitle:nil
                 otherButtonTitles: nil];
	[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector: @selector(performDismiss:)
								   userInfo:nil repeats:NO];
	[autoClearAlert show];
}

- (void) performDismiss: (NSTimer *)timer
{
	[autoClearAlert dismissWithClickedButtonIndex:0 animated:NO];
	[autoClearAlert release];
	autoClearAlert = NULL;
}

猜你喜欢

转载自aqbbsxiao.iteye.com/blog/1921856
今日推荐