单击UIAlertview上的按钮后,iOS应用程序立即崩溃 [英] The iOS app crashed right after clicking the button on a UIAlertview
问题描述
用户点击UIAlertview上的按钮后,我尝试使用手机应用程序拨号。手机应用程序确实已打开,但原始应用程序在单击UIAlertview上的按钮后立即崩溃。有人知道原因吗?我确实试图确保我发布了应该发布的所有内容。谢谢!以下是代码:
I tried to dial a number with the phone app after the user click on a button on a UIAlertview. The phone app did open, but the original app crashed right after clicking the button on the UIAlertview. Does anyone one know the reason? I did try to make sure I released everything that should be released. Thanks! Below is the code:
-(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上的按钮后仍然崩溃。错误是0x3beb85b0:ldr r3,[r4,#8] EXC_BAD_ACCESS(代码= 1,地址= 0x7269634f)任何帮助将不胜感激。谢谢!
but it still crashed after clicking the button on the UIAlertview. The error is 0x3beb85b0: ldr r3, [r4, #8] EXC_BAD_ACCESS (code=1, address=0x7269634f) Any help would be appreciated. Thanks!
推荐答案
由于此代码,崩溃正在发生。 [自我发布];
。
当您致电 self
release
显示警报的视图将被释放并取消分配,而不是alertView。这就是崩溃的原因。
The crash is happening due to this code. [self release];
.
When you call self
release
the view in which the alert is displayed will released and deallocated, not the alertView. That's the cause of crash.
您已经使用<$ c在 dialButtonPressed:
方法中释放alertViews内存$ c> [message autorelease];
You are already releasing the alertViews memory in the dialButtonPressed:
method using [message autorelease];
因此无需再次在 clickedButtonAtIndex $ c中发布alertView $ C>。所以更改方法如:
So no need to release the alertView again in clickedButtonAtIndex
. So change the method like:
- (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;
}
这篇关于单击UIAlertview上的按钮后,iOS应用程序立即崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!