简体   繁体   English

更改显示的窗口(Mac - Cocoa)

[英]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. 很抱歉,如果这是一个非常基本的问题,但我是XCode和Mac开发的新手。 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? 但是,当我点击按钮时,有没有办法让_window显示_login的内容?而不是关闭_window并打开_login窗口?

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. 通常你不需要多个窗口,你只需要另一个视图并交换它。

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. 您可以在xib文件中包含一个单独的视图,其中包含您在登录窗口中所需的内容,然后在您的窗口上调用setContentView:loginView,这将替换旧视图的旧视图。 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. 另一种方法是使用选项卡视图 - 如果您不喜欢选项卡视图的外观,则有一个tabless tabview看起来不像它有选项卡---您将在一个选项卡中有一个视图,并且另一个登录视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM