点击UIAlertview上的button后,iOS应用程序崩溃

在用户点击UIAlertview上的button后,我尝试用电话应用拨打一个号码。 手机应用程序确实打开了,但是点击UIAlertview上的button后,原来的应用程序崩溃了。 有人知道原因吗? 我确实试图确保我发布了应该发布的所有东西。 谢谢! 以下是代码:

-(IBAction)dialButtonPressed:(UIButton *)numberButton { if ([company isEqualToString:@"Not Found"]==true){ message = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"No replace number found. Would you like to dial anyway?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; message.tag = 0; if(phoneLinkString) { [phoneLinkString release]; phoneLinkString = nil; } [message show]; [message autorelease]; phoneLinkString = [[NSString stringWithFormat:@"tel:%@",replace]retain]; } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; [self release]; message = nil; if(message.tag == 0 && buttonIndex == 1){ NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString]; [[UIApplication sharedApplication] openURL:phoneLinkURL]; } - (void)dealloc { [phoneNumberString release]; [phoneNumberLabel release]; [self release]; [message release]; [super dealloc]; } 

最新的代码

  -(IBAction)dialButtonPressed:(UIButton *)numberButton { if ([company isEqualToString:@"Not Found"]==true){ message = [[UIAlertView alloc] initWithTitle:@"Sorry" message:@"No replace number found. Would you like to dial anyway?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil]; message.tag = 1; if(phoneLinkString) { [phoneLinkString release]; phoneLinkString = nil; } [message show]; [message autorelease]; phoneLinkString = [[NSString stringWithFormat:@"tel:%@",replace]retain]; } } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if(message.tag == 1 && buttonIndex == 1){ NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString]; [[UIApplication sharedApplication] openURL:phoneLinkURL]; message = nil; } } - (void)dealloc { [phoneNumberString release]; [phoneNumberLabel release]; [super dealloc]; } 

但点击UIAlertview上的button后,它仍然崩溃。 错误是0x3beb85b0:ldr r3,[r4,#8] EXC_BAD_ACCESS(代码= 1,地址= 0x7269634f)任何帮助,将不胜感激。 谢谢!

问题可能是您在if语句之前将您的警报设置为零。 尝试把它放在后面。

由于这个代码,崩溃正在发生。 [self release]; 。 当您调用self release ,显示警报的视图将释放并释放,而不是alertView。 这是导致崩溃的原因。

您已经使用[message autorelease];dialButtonPressed:方法中的alertViews内存[message autorelease];

所以不需要在clickedButtonAtIndex中再次释放alertView。 所以改变方法如下:

 - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { NSString *title = [alertView buttonTitleAtIndex:buttonIndex]; if(alertView.tag == 0 && buttonIndex == 1) { NSURL *phoneLinkURL = [NSURL URLWithString:phoneLinkString]; [[UIApplication sharedApplication] openURL:phoneLinkURL]; } message = nil; } 

你的崩溃是由内存pipe理不善导致的。 主要问题是调用[self release] 。 这是非常罕见的情况,这是适当的。

另一个问题是你试图在将message设置为nil之后检查message.tag 。 在nil对象上调用tag属性将始终导致值为0。

你的dealloc方法全错了。 不要打电话给[self release] 。 由于您在显示时自动释放,请勿调用[message release]

顺便说一句 – 永远不要使用0的tag 。这是默认的。 如果您想使用tag ,请始终使用非零值,以便可以将该值与默认值区分开来。