I have an app which utilises a set of custom "controls" which are loaded on demand from Xib files using methods similar to the below:
NSArray * topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"AudioPlayer" owner:self options:nil];
InteractiveMovieView *newAudio = [topLevelObjects objectAtIndex:0];
This approach works great except where there are multiple controls loaded at once (in effect on one "page" of the app).
Loading from the bundle each time is clearly inefficient but I can't find another way of approaching this. I've tried loading the nib into a copy
property once and returning it on demand for re-use, but that doesn't work as the copy returned is never a "clean" copy of the blank nib.
I hope that makes sense, and all help is appreciated.
It sounds like you're looking for the UINib
class. From the documentation:
Your application should use
UINib
objects whenever it needs to repeatedly instantiate the same nib data. For example, if your table view uses a nib file to instantiate table view cells, caching the nib in aUINib
object can provide a significant performance improvement.
Following Rob's suggestion, you could do the following:
@implmentation InteractiveMovieView (NibFactory)
+(id)movieView
{
static UINib * __nib ;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
__nib = [ UINib nibWithNibName:@"AudioPlayer" bundle:nil ] ;
});
InteractiveMovieView * view = [ __nib instantiateWithOwner:nil options:nil ][0] ;
return view ;
}
@end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With