What method of UIView gets called when instantiated from a NIB?

放肆的年华 提交于 2020-01-01 09:28:56

问题


I have a simple custom view that is connected via outlet to a NIB. For this particular view, there are actions that I would like to perform on the view when it is initialized, no matter what NIB it is on.

Trouble is, neither the (id)init or the (id)initWithFrame:(CGRect)frame methods are getting called on the custom view.

Which method gets called on a UIView when it is instantiated from a NIB? I would just use the view controller and viewDidLoad method except that this particular view appears on a lot of different NIBs.


回答1:


You can use awakeFromNib for this kind of initialization. The regular initialization methods are called when the object is actually created by IB and then archived using NSCoding, so those methods are never called within your application. You could also override initWithCoder: which will be called, but I don't recommend it since other outlets may not be wired at that point.



来源:https://stackoverflow.com/questions/2663333/what-method-of-uiview-gets-called-when-instantiated-from-a-nib

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