0

I have a Window with an NSTableView, a checkbox, and a label. These three items are wired to a NSWindowController subclass I named PrefWindowController:

@interface PrefWindowController : NSWindowController

//Members
@property (weak) IBOutlet NSButton *enabledCheckbox;
@property (weak) IBOutlet NSTextField *powerConnectedLabel;
@property (weak) IBOutlet NSTableView *ethernetAdaptorsTable;
@property (strong) EthernetAdaptorsDataSource* currentDatasource;


//Methods
-(IBAction)updateTable;
//...

//Overridden Methods
-(void) windowDidLoad;
@end

I overrode windowDidLoad and use that to set the DataSource on the NSTableView:

-(void)windowDidLoad
{
    [super windowDidLoad];
    [self updateTable:nil];
}
-(void) updateTable:(NSNotification*)notification
{
    self.currentDatasource = [[EthernetAdaptorsDataSource alloc] initWithData: [[AppState getState] ethernetAdaptors]];
    [self.ethernetAdaptorsTable setDataSource:self.currentDatasource];
    [self.ethernetAdaptorsTable reloadData];
}

I have a timer that eventually triggers a reload of the data. It's a little convoluted, but specifically the timer calls a function (checkForUpdates) and if an update occurs, it sends a Notification, which is listened for:

 [[NSNotificationCenter defaultCenter] addObserver:self
                                       selector:@selector(updateTable:)
                                       name:@"ethernetStatusChanged"
                                       object:nil];

The problem is when the notification fires, and we enter into updateTable, enabledCheckbox is a valid pointer, and powerConnectedLabel is a valid pointer, but ethernetAdaptorsTable is nil. It wasn't nil when we called updateTable via windowDidLoad, so I know it's got an appropriate 'Reference Outlet' (at least, I'm pretty sure - I'm a little confused between the difference of an "Object" in IB and "File's Owner"). But I don't know while it would be nil but the other two wouldn't.

Tom Ritter
  • 99,986
  • 30
  • 138
  • 174
  • I think I have an idea of what is going on. Can you please add a screenshot of IB with these elements? – Dumoko Feb 19 '14 at 14:46
  • Is this what you need? http://imgur.com/uLwfczd – Tom Ritter Feb 19 '14 at 14:52
  • I see that tableView comes like a separate node in the View hierarchy. This means that because tableView acts as a sort of root, it should take care of it's own memory by itself, so try setting the weak into strong reference for tableView. – Dumoko Feb 19 '14 at 14:59
  • I changed it to `@property (strong) IBOutlet NSTableView *ethernetAdaptorsTable;` but it has the same behavior/problem. – Tom Ritter Feb 19 '14 at 15:25
  • I thought my problem may have something to do with File Owner vs the object I have - but after following the advice in this question http://stackoverflow.com/questions/8590853/instance-variables-lose-value-after-initwithnib-and-before-ibaction-in-nsviewcon - that is, wiring everything to file owner, it makes it _worse_ as powerConnectedLabel and enabledCheckbox are now nil too! – Tom Ritter Feb 20 '14 at 14:26

2 Answers2

0

Instead of windowDidLoad use awakeFromNib to do that processing. I think windowDidLoad is called before all outlets are wired up.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
  • In windowDidLoad, all outlets are wired up (at least that has been the case so far.) But when the notification fires (later, unrelated to the windowDidLoad event) the outlet is not wired. – Tom Ritter Feb 19 '14 at 18:28
0

The problem I was having was essentially that I had some things wired up to File's Owner, and other things wired up to an NSObject that was itself the same class as FileOwner. There were two instances of this class floating around and I was winding up on different ones of them.

I debugged it by overriding and breakpointing the Init functions. I also referenced this question which had virtually the same problem: Instance variables lose value after initWithNib and before IBAction in NSViewController

Community
  • 1
  • 1
Tom Ritter
  • 99,986
  • 30
  • 138
  • 174