2

In my App Delegate, I load the "MainView" of the MainViewController as follows:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    
    MainViewController *mainvc = [[MainViewController alloc] initWithNibName:@"MainView" bundle:nil];
    [self.window setContentView:mainvc.view];
}

Within the "MainView", I have a button connected to the following IBAction method:

- (IBAction)switchToOneView:(id)sender {
    
    OneViewController *onevc = [[OneViewController alloc] initWithNibName:@"OneView" bundle:nil];
    [self.view.window setContentView:onevc.view];
}

I want to use this button to change the content view of the main window. Unfortunately this does not work and I receive the following error:

[NSLock switchToOneView:]: unrecognized selector sent to instance 0x6000000c5b00

Any suggestions on how to switch the content view of a NSWindow with the view of a NSViewController?

Here's a diagram of what I'm trying to accomplish:

enter image description here

A button in the "MainView" will change the content view to "OneView" or "TwoView". Once that view is loaded, I would like to click a button in that view to remove it and return back to the "MainView".

Community
  • 1
  • 1
wigging
  • 8,492
  • 12
  • 75
  • 117

1 Answers1

2

The error you're getting indicates, in most cases, a memory management bug. It means that the message switchToOneView: was sent to an object of class NSLock. Unsurprisingly, NSLock doesn't recognize that message.

So, to what target is the button which sends switchToOneView: connected? Whatever that is, it has been deallocated. Another object, an instance of NSLock has, by chance, been allocated at the address that used to hold the target.

I suspect the button was targeting the MainViewController. Since you don't continue to hold a strong reference to that controller after -applicationDidFinishLaunching:, ARC has fully released it, resulting in it being deallocated.

Running your app under the Zombies instrument would tell you for sure.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154