NSView initialization: -init: vs. -awakeFromNib:

自古美人都是妖i 提交于 2019-12-09 17:21:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!