Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Faster way to load Nib file from a bundle in iOS?

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.

like image 763
AustinRathe Avatar asked Jan 15 '23 05:01

AustinRathe


2 Answers

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 a UINib object can provide a significant performance improvement.

like image 110
rob mayoff Avatar answered Jan 25 '23 13:01

rob mayoff


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
like image 28
nielsbot Avatar answered Jan 25 '23 11:01

nielsbot