views:

102

answers:

1

In which UIViewController method should I set to nil all the occurrences of the view controller as a delegate? Is it viewDidUnload (too early?), dealloc (too late?) or something else?

A: 

To avoid EXC_BAD_ACCESS you should set delegate properties to nil in your dealloc. This guarantees that other objects won't try to send delegate messages to your object after it's been dealloced. For example, if your UIViewController has a webView property, for which it is a UIWebViewDelegate, you should do:

- (void)dealloc {
    self.webView.delegate = nil;
    self.webView = nil; // assuming @property (nonatomic, retain), or use [webView release] if you prefer.
    [super dealloc];
}

You could set the delegate to nil earlier if you want to stop receiving delegate messages for some other reason, but it's not necessary if you're just trying to avoid EXC_BAD_ACCESS.

cduhn