YMCalendarSheet icon indicating copy to clipboard operation
YMCalendarSheet copied to clipboard

Can you elaborate a bit more about using initWithCoder: instead of awakeAfterUsingCoder:

Open DavydLiu opened this issue 11 years ago • 4 comments
trafficstars

Hi Yang,

I was reading your blog post (http://blog.yangmeyer.de/blog/2012/07/09/an-update-on-nested-nib-loading/) about an update regarding nested nibs.

There is an update on 2013-03-10 says there is a better way to do this by using initWithCoder:. I am curious how to actually implement it; can you explain it in details?

Thanks!

DavydLiu avatar Jul 22 '14 14:07 DavydLiu

Hej,

Basically the idea is that you load the view from a Nib and add it as a subview to yourself. As my blog post says:

YMCalendarSheet code: In -initWithCoder: do [self addSubview:[[[UINib nibWithNibName:@"YMCalendarSheet" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0]]and similarly in -initWithFrame:.

This doesn't technically replace the placeholder view but rather embeds the actual view inside the placeholder. But it's less extravagant than -awakeAfterUsingCoder:.

HTH

Yang

My upcoming app: http://gemba.io — "git push assets" for designers.

On 22.07.2014, at 16:14, Xingruo Liu [email protected] wrote:

Hi Yang,

I was reading your blog post (http://blog.yangmeyer.de/blog/2012/07/09/an-update-on-nested-nib-loading/) about an update regarding nested nibs.

There is an update on 2013-03-10 says there is a better way to do this by using initWithCoder:. I am curious how to actually implement it; can you explain it in details?

Thanks!

— Reply to this email directly or view it on GitHub.

yangmeyer avatar Jul 23 '14 10:07 yangmeyer

Thanks! However, when I was trying to implement the "initWithCoder" part. It went into a infinite recursive calls. Any ideas on this? Should I add some conditions to jump out of this?

DavydLiu avatar Jul 23 '14 12:07 DavydLiu

I suspect that your EmbedView.nib is trying to instantiate the embedded view (using -initWithCoder:).

1.) In your Container.nib, add a EmbedView. This will instantiate the embedded view using -initWithCoder:. 2.) In EmbedView.nib, the root view should be a UIView (!), which may contain subviews. Connect the subviews outlets with the File Owner.

Note that, using this method, the view hierarchy is different from the -awakeAfterUsingCoder: approach from my blog post, because the subviews are not direct subviews of EmbedView. The UIView from the EmbedView.nib is embedded inside the EmbedView from the Container.nib.

yangmeyer avatar Jul 24 '14 07:07 yangmeyer

After I set the EmbedView.nib's File Owner to the view class, the infinite loop problem resolved. This approach does add another UIView into the view hierarchy.

Also, I wondering does this approach break the pattern (or convention) that File Owner should only be set to a view controller class instead of a view class?

Thanks!

DavydLiu avatar Jul 25 '14 16:07 DavydLiu