问题
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?
回答1:
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).
来源:https://stackoverflow.com/questions/5374076/nsview-initialization-init-vs-awakefromnib