LuckyLuke
LuckyLuke

Reputation: 49097

Creating reusable views in Storyboard

I have recently started to develop apps for the iOS platform after having a break from it. During the break I have been programming a lot of Java Swing applications and I am baffled by the fact that there are so hard to create reusable views in iOS.

Our team created small reusable GUI components for "everything". The container view that was introduced some versions ago doesn't help in all situations since you can't embed it in for instance a UITableViewCell if the table is dynamic.

People are writing about don't use XIB's anymore, use Storyboard (blah blah). It makes me worried when use code like this for my reusable views:

[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:self options:nil];
[self addSubview: self.contentView];

So I wonder, what is the state of reusable views by the end of year 2015? Is this the way to do it or are there better ways now?

(And of course I use Swift now, I am just copy-pasting from an old project)

Upvotes: 3

Views: 3010

Answers (1)

matt
matt

Reputation: 535353

People are writing about don't use XIB's anymore

Ignore that. What you're doing is the way to do it. A .xib file is just a way of encapsulating all the code you'd need to instantiate and configure a view and its subviews, which is exactly what you want.

The only thing I would do differently is to use UINib rather than NSBundle, because when you load using UINib you get caching, which saves time if you're going to load this nib multiple times.

(Of course, you could do it with a storyboard too. But that's overkill, because the contents of a storyboard always involve a UIViewController, and you don't need one in this situation.)

Upvotes: 8

Related Questions