简体   繁体   English

您如何使用 MainMenu.xib 中的视图?

[英]How do you work with views in MainMenu.xib?

I'm coming over from iOS development and I can't figure out this basic thing: if I open MainMenu.xib in Interface Builder and I drag a button onto the main window's view, what class should I connect that button's action to -- the App Delegate?我是从 iOS 开发过来的,我无法弄清楚这个基本的事情:如果我在 Interface Builder 中打开 MainMenu.xib 并将一个按钮拖到主窗口的视图上,我应该将该按钮的操作连接到哪个类 -应用程序委托?

If I want to add a View Controller, how do I "assign" it to the main window's view in MainMenu.xib?如果我想添加一个视图控制器,我如何将它“分配”给 MainMenu.xib 中的主窗口视图?

What is the pattern -- to use an NSViewController, to use an NSWindowController, or to just shove everything into the App Delegate?模式是什么——使用 NSViewController、使用 NSWindowController 还是将所有内容都推入 App Delegate?

So the default is that your main application window is an outlet in the app delegate.所以默认情况下,您的主应用程序窗口是应用程序委托中的一个出口。 You should keep MainMenu.xib 's owner as the app delegate.您应该保留MainMenu.xib的所有者作为应用程序委托。

A common alternative, if you are creating your own custom window controller, is to create a property in the AppDelegate of type CustomWindowController , then in the -applicationDidFinishLaunching , instantiate your custom window and show it:如果您要创建自己的自定义窗口控制器,一个常见的替代方法是在 AppDelegate 中创建一个CustomWindowController类型的CustomWindowController ,然后在-applicationDidFinishLaunching ,实例化您的自定义窗口并显示它:

   if(!windowController){
       windowController = [[MyWindowController alloc] initWithWindowNibName:@"MyWindow"];
    }

    [windowController showWindow:self]; 

Of course this means you can sever the connection to the default window they give you.当然,这意味着您可以切断与他们提供给您的默认窗口的连接。

If you add any buttons, you will want to connect them to the owner of the superview they are in. Whether that be the app delegate if you are working in the MainMenu.xib default window, or the custom window controller if you have created your own window, or a custom view controller if you are creating your own view.如果您添加任何按钮,您将希望将它们连接到它们所在的超级视图的所有者。如果您在MainMenu.xib默认窗口中工作,那么无论是应用程序委托,还是如果您已经创建了自定义窗口控制器,自己的窗口,或者自定义视图控制器(如果您要创建自己的视图)。

If you want to add a view controller, you first have to create the new Cocoa class as you would with a CocoaTouch class.如果要添加视图控制器,首先必须像使用 CocoaTouch 类一样创建新的 Cocoa 类。 Then in the Interface Builder, go to the Object Library at the bottom right and search for "Object".然后在界面生成器中,转到右下角的对象库并搜索“对象”。 Drag that into your Document Outline, and from there you can set up the connections from your view into your new view controller.将它拖到您的文档大纲中,然后您可以从那里设置从您的视图到新视图控制器的连接。

The pattern really depends on what you are doing, but at the end of the day for a basic app you won't need to create your own NSWindowController and you can work with the default window they give you through the AppDelegate.该模式实际上取决于您在做什么,但最终对于基本应用程序而言,您不需要创建自己的 NSWindowController,您可以使用它们通过 AppDelegate 提供的默认window If you are creating custom views that require some logic, then you will have to create your own custom NSViewControllers and bring them into your xibs如果您正在创建需要一些逻辑的自定义视图,那么您将必须创建自己的自定义 NSViewControllers 并将它们带入您的 xibs

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

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