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

前端 未结 3 1675
走了就别回头了
走了就别回头了 2021-01-21 17:33

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 butt

相关标签:
3条回答
  • 2021-01-21 17:34

    Your crash is being caused by poor memory management. The primary issue is calling [self release]. It's a pretty rare case that this is appropriate.

    Another issue is your attempt to check the message.tag right after setting message to nil. Calling the tag property on a nil object will always result in a value of 0.

    Your dealloc method is all wrong. Don't call [self release]. Don't call [message release] since you autoreleased it when you showed it.

    BTW - never use a tag of 0. This is the default. If you want to use the tag, always use a non-zero value so you can distinguish the value from the default.

    0 讨论(0)
  • 2021-01-21 17:41

    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.

    You are already releasing the alertViews memory in the dialButtonPressed: method using [message autorelease];

    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;
    }
    
    0 讨论(0)
  • 2021-01-21 17:54

    The problem might be that you set your alert to nil before your if statement. Try putting it after.

    0 讨论(0)
提交回复
热议问题