-1

this might be a dumb question but I found nothing to it... I stumbled over a piece of code that I think can be optimized.

I have a viewcontroller that consists of 24 notes/postits. the following gets called during the view-did-load-process:

 for (int i = 1; i <= 24; i++)
{
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PostitView" owner:self options:nil];
    for (UIView *subview in nib)
    {
        if ([subview isKindOfClass:NSClassFromString(@"PostitView")]) 
        {

it takes quite a while to load the postit again and again from the nib.

I would have said, I simply make a copy from the subview, but it wont work. so whats the right way then?

thanks!!! Tom

Tom
  • 213
  • 3
  • 10
  • check this answer http://stackoverflow.com/questions/4036398/iphone-create-a-reusable-component-control-that-has-some-interface-builder-pi/4037281#4037281 – Aziz Feb 12 '13 at 17:23
  • there is no difference to what I am doing. Apparently this is not possible when using NIB's. – Tom Feb 12 '13 at 17:33
  • how long time does it take to load your view? – Aziz Feb 12 '13 at 17:37
  • well it takes too long(in my and the performance profiler opinion). its just alright if I must do it just once. but I am planning to extend this viewcontroller, so these loadFromNibview could happen quite frequently – Tom Feb 12 '13 at 17:56
  • did you try to add it to a new xib which has only PostitView then you don't have to loop through all views, – Aziz Feb 12 '13 at 18:01

1 Answers1

2

Before the loop, create a UINib object representing your nib. Inside the loop, ask the UINib to instantiate itself.

UINib *nib = [UINib nibWithNibName:@"PostitView" bundle:nil];
Class viewClass = [PostitView class];
for (int i = 0; i < 24; i++) {
    NSArray *topLevelObjects = [nib instantiateWithOwner:self options:nil];
    for (UIView *subview in topLevelObjects) {
        if ([subview isKindOfClass:viewClass)  {
            ...
rob mayoff
  • 375,296
  • 67
  • 796
  • 848