23

I would like to know if there is any difference between calling [super viewDidUnload] before realeasing properties or after it.

Thank you!

  self.webView = nil;
  self.fullText = nil;
  [super viewDidUnload];

or

  [super viewDidUnload];
  self.webView = nil;
  self.fullText = nil;
arielcamus
  • 773
  • 1
  • 8
  • 24
  • related: http://stackoverflow.com/questions/2148450/viewdidload-unload-messages-to-super – cregox Jan 12 '11 at 12:40
  • 1
    possible duplicate of [`[super viewDidLoad]` convention](http://stackoverflow.com/questions/844195/super-viewdidload-convention) – jscs Sep 28 '11 at 00:30

5 Answers5

12

This depends on what the superclass does in viewDidUnload. If it's just a standard UIViewController, either will do because -[UIViewController viewDidUnload] does nothing.

rpetrich
  • 32,196
  • 6
  • 66
  • 89
  • 1
    what about the other case - i.e. if `[super viewDidUnload]` is not a standard `UIViewController` and does something in `viewDidUnload`? – matm Mar 23 '11 at 11:22
  • 5
    @delirus: For methods that "clean up", the super's implementation is usually called after. Similarly, methods that "initialize" should call super first. – rpetrich Mar 25 '11 at 23:02
10

I never thought it mattered, but we just found a bug where the app crashed calling it first. The trend at my office has been to call it last, but the Apple template puts it first.

You can do what you want, but I will always call it last from now on.

ZaBlanc
  • 4,679
  • 1
  • 35
  • 38
  • 1
    Interestingly, if you drag outlets from IB to code behind, it inserts the nil settings *before* the super call...makes no sense given the templates and the position of the comments. – typeoneerror Aug 02 '11 at 06:42
  • Experienced the same as @ZaBlanc. Crash! – raheel Sep 29 '11 at 22:34
  • Though the template puts it first, I always put it last, as a safety feature, just in case super unloads something that may muck-up the program. – ArtSabintsev Oct 24 '11 at 01:12
6

This question is not fully answered. (And it's first hit when I google on this topic, so a complete answer would be useful to me and others.)

If the superclass DOES something in viewDidUnload, then what is the correct order for calling it in your class's viewDidUnload: before or after releasing properties?

For the record, it seems the consensus is to call the super method LAST:

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

SteveCaine
  • 512
  • 4
  • 13
2

This only matters if the superclass of your view actually does something in viewDidLoad. The default UIViewController doesn't do anything in viewDidLoad so if your view is a standard UIViewController then you can put [super viewDidLoad] anywhere in the viewDidLoad method. However, if super does something in viewDidLoad, then it does matter where you put [super viewDidLoad].

conorgriffin
  • 4,282
  • 7
  • 51
  • 88
2

This is just like the dealloc method.

- (void)dealloc
{
    [array release];
    [super dealloc];
}

So I think we should call [super viewDidUnload] last.

0x8badf00d
  • 6,391
  • 3
  • 35
  • 68
nonamelive
  • 6,510
  • 8
  • 40
  • 47