繁体   English   中英

iOS应用程序在单击UIAlertview上的按钮后立即崩溃

[英]The iOS app crashed right after clicking the button on a UIAlertview

用户点击UIAlertview上的按钮后,我尝试使用手机应用程序拨号。 手机应用程序确实打开了,但是原始应用程序在点击UIAlertview上的按钮后立即崩溃。 有人知道原因吗? 我确实尝试确保发布了应该发布的所有内容。 谢谢! 以下是代码:

-(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)任何帮助将不胜感激。 谢谢!

问题可能是您在if语句之前将警报设置为nil。 试着把它放进去。

由于此代码,崩溃正在发生。 [self release]; 当您调用self release ,将显示和取消分配显示警报的视图,而不是alertView。 这是崩溃的原因。

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

因此,没有必要再次释放alertView clickedButtonAtIndex 所以改变方法如:

- (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;
}

您的崩溃是由内存管理不善引起的。 主要问题是调用[self release] 这是一个非常罕见的情况,这是合适的。

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

你的dealloc方法都错了。 不要打[self release] 不要调用[message release]因为您在显示时自动释放它。

BTW - 永远不要使用0的tag 。这是默认值。 如果要使用tag ,请始终使用非零值,以便将值与默认值区分开来。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM