Crystal
Crystal

Reputation: 29458

Setting delegate to nil in iOS 6

Prior to iOS 6, we were supposed to

- (void)viewDidUnload {
   self.someDelegate = nil;
   [super viewDidUnload];
}

Now that viewDidUnload is deprecated, where do we set our delegates to nil? Thanks!

Upvotes: 1

Views: 986

Answers (3)

AliSoftware
AliSoftware

Reputation: 32681

Views are no longer unloaded when receiving a memory warning in iOS6. So the view is never unloaded on such cases and viewDidUnload is never called in iOS6.

If you really want to mimic the old behavior (unloading the view upon receiving a memory warning) you now have to implement this behavior in the didReceiveMemoryWarning method of your view controller, after testing that the self.view.window property is nil (meaning the view is not on screen anymore, thus will be "unloaded", meaning that you are in the same situation as the old viewDidUnload case).

-(void)didReceiveMemoryWarning
{
    if (self.isViewLoaded && !self.view.window)
    {
        // If view already loaded but not displayed on screen at this time (not attached to any window) then unload it
        self.view = nil;

        // Then do here what you used to do in viewDidUnload
        self.someDelegate = nil;
        ...
    }
    [super didReceiveMemoryWarning];
}

But note that if you use ARC and iOS5+, you generally won't need to set your delegate to nil anymore, thanks to the weak property attribute and the Zeroing-Weak-References mechanism that automatically reset weak variables and properties to nil if the object they are pointing to does not exist anymore (thus avoiding dangling pointers).

[EDIT] And as explained by @Martin R in the comments, views are not unloaded anymore when receiving a memory warning in iOS6, so you won't have to manage this case of receiving a memory warning and think about releasing your delegate there as this use case won't occur anymore in iOS6.

Upvotes: 4

user102008
user102008

Reputation: 31303

Prior to iOS 6, we were supposed to

  • (void)viewDidUnload { self.someDelegate = nil; [super viewDidUnload]; }

Where did you hear this?

Do you even know what viewDidUnload is for prior to iOS 6?

viewDidUnload is only called in low memory situations, which cause the view to be unloaded. It will never be used during normal operation. If you were depending for it to be called to do other things, that is wrong.

Plus, why would you ever need to set self's delegate to nil anyway? "Niling the delegate" refers to setting other objects' delegates (which point to self) to nil when self is deallocated. Setting self's delegate makes no sense (why would you care setting stuff on self if self is no longer used?).

Upvotes: 1

minjiera
minjiera

Reputation: 336

Well, I did not come up with this myself, but if should help you: "In iOS 6, the viewWillUnload and viewDidUnload methods of UIViewController are now deprecated. If you were using these methods to release data, use the didReceiveMemoryWarning method instead. You can also use this method to release references to the view controller’s view if it is not being used. You would need to test that the view is not in a window before doing this."

http://www.bgr.com/2012/06/11/ios-6-beta-download-link-iphone-ipad-ipod-touch-release/

Upvotes: 3

Related Questions