Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NibLoadable subviews are nil in initializer #65

Closed
nsoojin opened this issue Aug 22, 2018 · 3 comments
Closed

NibLoadable subviews are nil in initializer #65

nsoojin opened this issue Aug 22, 2018 · 3 comments

Comments

@nsoojin
Copy link

nsoojin commented Aug 22, 2018

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?

@AliSoftware
Copy link
Owner

AliSoftware commented Aug 22, 2018

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.

@AliSoftware
Copy link
Owner

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.

@nsoojin
Copy link
Author

nsoojin commented Aug 27, 2018

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

@nsoojin nsoojin closed this as completed Aug 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants