41

If i implement my own version of awakeFromNib, should I call [super awakeFromNib] at the end of my method?

Sviatoslav Yakymiv
  • 7,887
  • 2
  • 23
  • 43
cfischer
  • 24,452
  • 37
  • 131
  • 214

3 Answers3

94

awakeFromNib for UIKit (iOS):

You must call the super implementation of awakeFromNib to give parent classes the opportunity to perform any additional initialization they require. Although the default implementation of this method does nothing, many UIKit classes provide non-empty implementations. You may call the super implementation at any point during your own awakeFromNib method.

awakeFromNib for AppKit (Mac):

(not true anymore, if using OS X 10.6 or higher)

You should call the super implementation of awakeFromNib only if you know for certain that your superclass provides an implementation. Because the Application Kit does not provide a default implementation of the awakeFromNib method, calling super results in an exception if the parent class does not implement it. Classes whose immediate parent class is NSObject or NSView do not need to call the super implementation. For any other classes, you can use the instancesRespondToSelector: class method of NSObject to determine if the parent class responds to awakeFromNib and call the method if it does.

Colas
  • 3,473
  • 4
  • 29
  • 68
ma11hew28
  • 121,420
  • 116
  • 450
  • 651
  • 1
    I guess it's only in the release notes, rather than the docs, that NSObject implements -awakeFromNib as of OS X 10.6 – Mike Abdullah May 29 '12 at 15:14
  • @MikeAbdullah You're correct, the OS X docs don't mention this yet. I've filed radar://22585685 to update the docs, and radar://22585539&22585540 to add NS_REQUIRES_SUPER to the declarations in UIKit and AppKit. :-) – Quinn Taylor Sep 04 '15 at 23:29
7

The documentation covers that perfectly.

If you meant to ask about Cocoa Touch, you're not so lucky: The UIKit documentation doesn't answer the question definitively anywhere that I could find. Best I can suggest would be to follow the same rules as in Cocoa.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • In this post, http://stackoverflow.com/questions/1588813/why-wont-my-awakefromnib-fire, the last answer does point to this information (section 4 of http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html) – KevinDTimm Oct 21 '10 at 16:44
  • KevinDTimm: I don't see anywhere on that documentation page that specifies whether one should send `[super awakeFromNib]`. Plenty of discussion of `awakeFromNib` for both Cocoa and Cocoa Touch, but none on that particular aspect. – Peter Hosey Oct 21 '10 at 17:07
  • I'm only commenting on the question about whether cocoa and cocoa touch respond to the same rules - it appears that they do not. – KevinDTimm Oct 21 '10 at 17:52
  • True, but the only difference I can see is that Cocoa Touch only sends `awakeFromNib` to objects that have just been unarchived, whereas Cocoa also sends it to the File's Owner. – Peter Hosey Oct 21 '10 at 18:33
1

Yes you should.

I implemented drag and drop and everything was working until I added my own awakeFromNib then the drag and drop function whichTypeToProcess never got called.

It wasn't till I added [super awakeFromNib]; as the last statement in my own awakeFromNib that the drag and drop function whichTypeToProcess was being called again and drag and drop started working again.

FYI - This was in a MacOSX application.