Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSView initialization: -init: vs. -awakeFromNib:

I have a simple class MyView inherited from NSView and instance variable NSImage * image; in it. Class functionality is to draw image on the view.

However, in -drawRect: image instance always equal nil, if it was initialized in -init: function and not nil if it was initialized in -awakeFromNib:. Log shows both -init: and -awakeFromNib: functions are called only once.

Does -awakeFromNib: reinitialize GUI component by default?

like image 573
Gunnar Schigel Avatar asked Mar 21 '11 05:03

Gunnar Schigel


1 Answers

No. There is no default implementation for awakeFromNib.

Your problem is that you're overriding the wrong initialization method. An NSView's designated initializer is initWithFrame:. Your subclass's plain init never gets called, and that is why your image ivar doesn't point to anything.

You can initialize that ivar in either awakeFromNib or initWithFrame:. It makes no difference. The former is called after the latter, and it is provided as a point where you can insert initialization code that needs to interact with other objects in your nib (via your IBOutlets).

like image 118
jscs Avatar answered Oct 05 '22 10:10

jscs