0
votes

I'm really new to Mac development (although I have lots of iOS experience) and I'm trying to switch between NSViewControllers in a NSWindow. It's really simple: when a button is pressed, show the second view and hide the first one. Here's my code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    firstView = [[FirstView alloc] initWithNibName:@"FirstView" bundle:nil];
    [_window setContentView:menu.view];
}

- (IBAction)openSecondView:(id)sender {
    secondView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil];
    [_window setContentView:game.view];
}

I made sure that the method is called, and that secondView is loaded correctly. What's the problem here?

1

1 Answers

3
votes

An easier approach is load both views into your content view and adjust only the alpha value.

- (void) switchToFirstView: (id) sender {
    [[_secondView animator] setAlphaValue: 0.0f];
    [[_firstView animator] setAlphaValue: 1.0f];
}

- (void) switchToSecondView: (id) sender {
    [[_secondView animator] setAlphaValue: 1.0f];
    [[_firstView animator] setAlphaValue: 0.0f];
}

I took the liberty of using the animator to make the transition fade above but you could also go without it if you prefer an instant switch.