开发者

NSView initialization: -init: vs. -awakeFromNib:

开发者 https://www.devze.com 2023-02-17 21:28 出处:网络
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.

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 onc开发者_JS百科e.

Does -awakeFromNib: reinitialize GUI component by default?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号