cocoadeprecatedloadnibnamed

Cocoa NSBundle loadNibNamed deprecated


I'm developing a Cocoa App and I noticed NSBundle loadNibNamed is deprecated.

I'm trying to use a Sheet to show some config options. I'm using an AppController and the Config Sheet is a NIB created separately.

This is my code.

- (IBAction)showConfig:(id)sender{

   if (!_config) {
      [NSBundle loadNibNamed:@"Config" owner:self];
   }
   [NSApp beginSheet:self.config modalForWindow:[[NSApp delegate] window] modalDelegate:self didEndSelector:NULL contextInfo:NULL];
}

Using that code, the config Sheet opens and closes perfectly.

When I switch this [NSBundle loadNibNamed:@"Config" owner:self]; to [[NSBundle mainBundle] loadNibNamed:@"Config" owner:self topLevelObjects:nil]; the config Sheet still works fine.

My real problem is when I want to close it. The app crashes throwing this error:

Thread 1:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)

This is my IBAction to close the config Sheet.

- (IBAction)closeConfig:(id)sender{
   [NSApp endSheet:self.config];
   [self.config close];
   self.config = nil;
}

Once I skip the deprecated line, what do I need to do to close the config sheet correctly?

I'm running Yosemite and Xcode 6.4.


Solution

  • Is the window property in your app delegate class weak? If so, see this answer. The not-deprecated method that you are now using requires your controller to have strong references to the top level objects.