Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

awakeFromNib method called multiple times

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.

like image 539
Luca Bartoletti Avatar asked Aug 17 '11 11:08

Luca Bartoletti


People also ask

When awakeFromNib is called?

awakeFromNib gets called after the view and its subviews were allocated and initialized. It is guaranteed that the view will have all its outlet instance variables set.

What does awakeFromNib do?

awakeFromNib()Prepares the receiver for service after it has been loaded from an Interface Builder archive, or nib file.


2 Answers

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;
}
like image 158
Stephan Avatar answered Sep 21 '22 16:09

Stephan


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

like image 35
Luca Bartoletti Avatar answered Sep 20 '22 16:09

Luca Bartoletti