AliSoftware/Reusable

NibLoadable subviews are nil in initializer

nsoojin opened this issue · 3 comments

Hi. I made a NibLoadable view and added subviews.
However, those views are nil inside required init?(coder:) so I can't setup my views. (e.g. I want to set corner radius to a subview)

Where can I put additional init logic for NibLoadable?

Are they still nil in awakeFromNib?
When unarchiving a nib, iOS/macOS first creates all instances of views etc found in the nib, calling init(coder:) on each (sub)view classes to create each instance… and only once all instances are created, link them together by connecting the outlets between them, and call awakeFromNib once all is set up. That's how NIBs work in general.

This is not related/restricted to Reusable btw, this behavior is true whenever you use XIBs, with or without Reusable: by the time init(coder:) is called, outlets are not yet connected so they are always nil. That's why you set up views in awakeFromNib, (or alternatively in the case of ViewControllers, more commonly in viewDidLoad), not in init.

@AliSoftware Thank you so much for the detailed explanation. It helped me understand the loading of the views.