Home > Mobile >  Inconsistent awakeFromNib behavior when adding NSToolbar
Inconsistent awakeFromNib behavior when adding NSToolbar

Time:09-05

I am following a tutorial on creating a table view. It shows how to define two functions, numberOfRowsInTableView and viewForTableColumn, as well as wiring up the table to the AppDelegate (dataSource and delegate).

The tutorial -- as written -- works just fine. I can see the data presented with no issue. But, later I was playing around with adding a toolbar (NSToolbar) to the window, and now the application no longer acts in a consistent fashion.

Before I added the toolbar, awakeFromNib would always be called before numberOfRowsInTableView. This was a good thing because awakeFromNib is where the tutorial said to add the contents for the table... So if it is not done first, then the table contents will be empty.

Now after adding the toolbar to the window, numberOfRowsInTableView always gets called before awakeFromNib! And even more bizarre, sometimes I will launch the application and it will get called a second time. Other launches, it will only get called once. When it gets called a second time, I see the table data, because the second call will have occurred after awakeFromNib. But when it only gets called once, it always gets called before awakeFromNib and therefore the table shows no content:

App Launch Number Result
1 numberOfRowsInTableView is called once, before awakeFromNib (no data shown)
2 numberOfRowsInTableView is called twice; once before and once after awakeFromNib (data is shown)
3 numberOfRowsInTableView is called twice; once before and once after awakeFromNib (data is shown)
4 numberOfRowsInTableView is called twice; once before and once after awakeFromNib (data is shown)
5 numberOfRowsInTableView is called twice; once before and once after awakeFromNib (data is shown)
6 numberOfRowsInTableView is called twice; once before and once after awakeFromNib (data is shown)
7 numberOfRowsInTableView is called twice; once before and once after awakeFromNib (data is shown)
8 numberOfRowsInTableView is called once, before awakeFromNib (no data shown)
9 numberOfRowsInTableView is called twice; once before and once after awakeFromNib (data is shown)
10 numberOfRowsInTableView is called twice; once before and once after awakeFromNib (data is shown)
11 numberOfRowsInTableView is called once, before awakeFromNib (no data shown)
12 numberOfRowsInTableView is called once, before awakeFromNib (no data shown)

As soon as I delete the toolbar from the window, the behavior reverts back to normal and awakeFromNib is always called before numberOfRowsInTableView. I even created a brand new project in Xcode to replicate this behavior and indeed, it behaves exactly as the other project.

Here are some code snippets:

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
    NSLog(@"numberOfRowsInTableView is called");
    return self.songs.count;
}
- (void)awakeFromNib
{
    self.songs = [NSMutableArray array];
    
    Song *aSong;
    
    aSong = [[Song alloc] init];
    aSong.title = @"Gaeta's Lament";
    aSong.duration = 289;
    
    [self.songsController addObject:aSong];
    
    aSong = [[Song alloc] init];
    aSong.title = @"The Signal";
    aSong.duration = 309;
    
    [self.songsController addObject:aSong];
    
    aSong = [[Song alloc] init];
    aSong.title = @"Resurrection Hub";
    aSong.duration = 221;
    
    [self.songsController addObject:aSong];
    
    aSong = [[Song alloc] init];
    aSong.title = @"The Cult of Baltar";
    aSong.duration = 342;
    
    [self.songsController addObject:aSong];
    
    NSLog(@"awakeFromNib is called");
}

Is there something special about adding an NSToolbar that would cause this kind of behavior? I thought awakeFromNib should always get called first?

CodePudding user response:

So it seems that the tutorial led me astray as it omitted a key ingredient to populating the NSTableView contents: one should always call reloadData after initializing the data source.

After doing the above (calling reloadData at the end of awakeFromNib), the table contents are always shown at application launch, regardless of whether or not I have added a toolbar to the window.

CodePudding user response:

First of all, your table view will depend on your data - in here your list of songs. Because of you make your view from nib so the init data will automatically blank because no data was there at first. That causes your problem.

Any time you update your data like insert/ update, the table view doesn't update it automatically, so you need to call method reloadData for the tableview to update.

[tableView reloadData];
  • Related