haseo98
haseo98

Reputation: 837

Change what window displays (Mac - Cocoa)

Sorry if this is a really basic question but I'm new to XCode and Mac development in general. I want to know how to get a window to change its contents without having to close and open a new window. For example:

- (IBAction)handlelock:(id)sender {
  [_window orderOut:sender];
  [_loginwindow makeKeyAndOrderFront:sender];
}  

But rather than closing _window and opening _login window, is there a way to make _window display the contents of _login when I click the button?

Upvotes: 1

Views: 1209

Answers (2)

Vervious
Vervious

Reputation: 5569

Or, just swap the window's content views:

- (IBAction)handlelock:(id)sender {
     NSView *previousContentView = _window.contentView;
     _window.contentView = _loginwindow.contentView;
     _loginwindow.contentView = previousContentView; // just store it in the other window
} 

Usually you wouldn't need multiple windows for this though, you would just have another view and swap it in.

Upvotes: 2

rdelmar
rdelmar

Reputation: 104092

There are several ways to do this. You could have a separate view in your xib file that contains what you want in your login window, and then call setContentView:loginView on you window, and that will swap out the old view for the new one. You would want to have a property that points to the original view, if you want to be able to switch back to it.

Another way is to use a tab view -- if you don't like the looks of a tab view, there is a tabless tabview that doesn't look like it has tabs --- you would have one view in one tab, and the login view in another.

Upvotes: 1

Related Questions