Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 555065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T11:46:35+00:00 2026-05-13T11:46:35+00:00

If learned the hard way that you should remove the delegate from an object

  • 0

If learned the hard way that you should remove the delegate from an object if the life span of the delegate is shorter than the object. But how do you do this if you don’t have a reference to the object anymore?

In my iPhone application I have a view controller vc which performs an asynchronous activity and is displayed as a modal view. A cancel button dismisses the modal view. If an error occurs, an UIAlertView alert is displayed. If the user taps on ok, both alert and the modal view disappear. Therefore vc is set
as delegate for alert and implements alertView:didDismissWithButtonIndex:. Something like this:

// UIViewController vc
    ...
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Error"
                          message:@"Something went wrong"
                          delegate:self
                          cancelButtonTitle:@"OK" 
                          otherButtonTitles:nil];
    self.alertView = alert; // needed to unset alertView.delegate in dealloc
    [alert show];
    [alert release];
    ...
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
        [self dismissModalViewControllerAnimated:YES];
    } 
}

Normally, the alert view blocks all input. Unfortunately, it fails to do so in some edge cases.
If the user touches down on the cancel-button before the alert view appears and touches up after the alert view appears, the view is dismissed, but not the alert. vc gets deallocated and if the user then taps “ok” on the alert, the application crashes because a message was sent to the released object.

I solved this by assigning alert to a property of vc so I can set alert.delegate to nil in dealloc. I find this solution to be not very elegant, because I don’t really need the reference to alert.

Is there a better approach?

Edit: Added the text in italics as clarification

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T11:46:36+00:00Added an answer on May 13, 2026 at 11:46 am

    Although, normally an alert view is presented over non changing content. So if the delegate is alive when the view appears it will likely be alive when it’s dismissed. If that’s not the case, you have to do exactly what you did, and unset the alert view’s delegate manually if you no longer care about it’s result.

    So you do care about the alertview since you care about it’s delegate method. The wrinkle is that the delegate may not apply by the time the alert is dismissed. So you need logic there, and for that logic you need to save a reference to the alert view in question.

    In other words, you are doing it right. Although, it may have been helpful if UIAlertView retained it’s delegate, but it doesn’t seem like it does if it crashes when it’s dismissed.

    Lastly, I thought alert view blocked all screen input? If not, you can make it truly modal by setting vc.view.userInteractionEnabled = NO when the alert appears and switch it back when it’s dismissed. This way the user can’t dismiss the controller while the alert view is up. Which sounds a bit more sane to me.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Now that i learned how to pass values to an SWF object via flashvars
I recently learned about Recursive Common Table Expressions (CTEs) while looking for a way
I recently learned about the , operator and the fact that it introduces a
Today I just learned that Adobe Air has a local SQL database, which is
I've learned a lot of programming language, and I think that Java or C#
I'm a java developer, not seasoned, but I am familiar with most concepts reasonably
I am considering starting a project so that I can learn more and keep
I recently learned about kayak , an HTTP server written in C# (i.e. not
I've recently learned OOP in PHP,.. and I though I'd try to write a
In Xcode (specifically v3.2.4), there are two ways of adding an external framework to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.