12

In my NSPersistenDocument based project i have a structure like this

myDocument (NSPersistentDocument) -> myDocument.xib (windows xib)
                                           |
                                           |-> view (the self.view) --> ... "other view"
                                           |
                                           |-> some NSArrayController 
                                           |
                                           |-> myResourceViewController --> myResourceViewController.xib
                                                                                          |
                                                                                          |-> view (the self.view)
                                                                                          |
                                                                                          |-> myTreeController (a NSTreeController subclass)

basically, myResourceViewController is an instance of a viewController who manage resourceView and manage their data.

in awakeFromNib method of myDocument i have the following code

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    ...
    [leftBar addSubview:resourceViewController.view]; //i add resourceViewController's view 
    resourceViewController.view.frame = leftBar.bounds;
    ...
}

in myResourceViewController awakeFromNib methods i have:

-(void)awakeFromNib;
{
    NSLog(@"%@", [self description]);

    [removeButton bind:@"enabled" toObject:resourceTreeController withKeyPath:@"selection" options:[NSDictionary dictionaryWithObject:NSIsNotNilTransformerName forKey:NSValueTransformerNameBindingOption]];

    NSArray *draggedTypes = [NSArray arrayWithObjects:ResourceURIPasteBoardType, nil];
    [resourceOutlineView registerForDraggedTypes:draggedTypes];
}

the NSLog say that awakeFromNib, of the same instance of myResourceViewController, is called 4 time, i don't understand why. My only ResourceViewController is created in myDocument xib. I don't use NSNib loading methods everywhere.

4
  • Are you sure it is the same object? Try NSLog(@"%p %@", self, [self description]). Aug 17, 2011 at 12:01
  • Yes, i check with NSLog(@"%@", [self description]); Aug 17, 2011 at 12:03
  • Is myDocument.xib is the only nib? Aug 17, 2011 at 15:14
  • No, myResourceViewController have a xib called myResourceViewController.xib. Aug 17, 2011 at 15:35

7 Answers 7

32

I found the solution. awakeFromNib is called every time a NSTableCellView is created by NSOutlineView.

10
  • Ok, it was so obvious in my case that awakeFromNib is called # of cells times .. But, what's the work around? Subclass NSTableCellView?
    – Mazyod
    Mar 30, 2012 at 2:02
  • 3
    Very infuriating, anyone know how to stop this? Sep 25, 2012 at 17:16
  • 5
    It's a workaround, but I set a BOOL initialize = NO in my init, then only execute the code in awakeFromNib when initialize = NO (and set it to YES within that if statement)
    – Bob Vork
    Sep 28, 2012 at 13:16
  • On 10.8 you can use registerNib:forIdentifier: so that it will be a different nib loading the cellview. Jan 5, 2013 at 23:55
  • 10
    @Mazyod The solution is not to set the owner to self in makeViewWithIdentifier:owner:
    – IluTov
    Apr 4, 2014 at 8:44
8

The root cause is described in the NSTableView header file of the method makeViewWithIdentifier: " .... Note that 'owner' will get an 'awakeFromNib:' call each time the object is instantiated."

My solution is simple but I expect not suitable for all: Just define e.g. the tabelView as owner:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSTableCellView *view = [tableView makeViewWithIdentifier:kTextViewIdentifier owner:tableView];

    return view;
}
1
  • 3
    When loading a prototype view it is admissible to simply pass nil for the owner. Dec 19, 2013 at 12:13
4

I don't know why it's called four times, but at least I can account for two calls of awakeFromNib. It's important to remember that awakeFromNib is called even for the File's Owner of the nib file, not only the objects contained in the nib file.

Therefore, your ResourceViewController's awakeFromNib gets called at least twice: when it is loaded in myDocument.xib, and then when the view managed by the ResourceViewController is loaded from another nib.

It's better to perform the initialization in other methods which is called on a more definite timing, such as ...didLoad or applicationDidFinish....

1

I put the code within a synchronized block inside my awakeFromNib like so.

e.g

@implementation {
    BOOL _initialize;
}

    - (id)init {
        self = [super init];
        if (self) {
            _initialize = YES;
        }
        return self;
    }

    - (void)awakeFromNib {
        @synchronized(self) {
            if (_initialize) {
                _initialize = NO;

                /* code to execute once */
            }
        }

        /* code to re-execute */
    }
}
0

I noticed the same in NSTableView. NSTableView was updated through NSArrayController and I noticed that NSTableView had delegate set to File Owner, When I removed delegate to File Owner, awakeFromNib called only once.

0

This wasn't easy to figure out but for me somehow I my table view changed to 'View Based' vs 'Cell Based' for the Content Mode.

Switching back to 'Cell Based' and the awakeFromNib only ran once.

Note: select your table view in Interface Builder three times to get to the right level. Or, just select your 'Table View' from the Documents Outline.

Xcode Attributes Inspector

1
  • I'm using xcode 5.0.2 and I can't find the property "Content Mode" of my table, any idea?
    – Sawsan
    Jan 19, 2014 at 9:51
0

The solution is not to set the owner to self in makeViewWithIdentifier:owner:

This makes the awakefromNib to be called multiple times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.